home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / DBio / DSeqDoc.cpp < prev    next >
Encoding:
Text File  |  1995-12-17  |  68.7 KB  |  2,813 lines  |  [TEXT/R*ch]

  1. // DSeqDoc.cp
  2.  
  3. #define MASKS 1
  4.  
  5. #include "DSeqDoc.h"
  6. #include <ncbi.h>
  7. #include <dgg.h>
  8. #include <Dvibrant.h>
  9. #include <DApplication.h>
  10. #include <DClipboard.h>
  11. #include <DControl.h>
  12. #include <DWindow.h>
  13. #include <DRichViewNu.h>
  14. #include <DTableView.h>
  15. #include <DViewCentral.h>
  16. #include <DTask.h>
  17. #include <DTracker.h>
  18. #include <DMenu.h>
  19. #include <DUtil.h>
  20. #include <DFindDlog.h>
  21. #include "DSeqFile.h"
  22. #include "DSeqEd.h"
  23. #include "DSeqMail.h"
  24. #include "DSeqCmds.h"
  25. #include "DSeqChildApp.h"
  26. #include "DSeqPrint.h"
  27. #include "DSeqPict.h"
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. Global char* gDefSeqName = NULL;
  35. static char gDefSeqNameStore[128];
  36. Boolean  DSeqDoc::fgTestSeqFile = false; // turn on/off seq-test for OpenDocument()
  37. Boolean  DSeqDoc::fgStartDoc = true;
  38. Boolean  DSeqDoc::fgUseColor = false;  
  39. Boolean  DSeqDoc::fgLockText = false;  
  40. short         DSeqDoc::fgViewMode = DAlnView::kModeSlide;
  41. Nlm_RecT DSeqDoc::fgWinRect = { 0, 0, 0, 0 };
  42.  
  43. Local short gAlnCharWidth = 12;
  44. Local short gAlnCharHeight = 15;
  45. //Local short gMinCommonPercent = 0;
  46. Local Boolean gSwapBackground = false;
  47.  
  48.  
  49. #define ETEXT 1
  50.  
  51. #define DAlnTextClass  DTextLine
  52. //  Nlm_TextLine > DTextLine -- new variant of DHiddenText
  53. //  DEditText or DHiddenText ?? -- EditText has box around, HiddenText no box
  54. //    DDialogScrollText( id, itsSuperior, width, height, gSeqFont, false) // true==wrap
  55. //    DTextInScroller(long id, DView* itsSuperior, short width, short height, 
  56. //        Nlm_FonT font, Boolean wrap);
  57.  
  58. extern "C" void Nlm_HScrollText (Nlm_BaR sb, Nlm_GraphiC t,
  59.                                Nlm_Int2 newval, Nlm_Int2 oldval);
  60. extern "C" void Nlm_DoActivate(void* g, Nlm_Boolean saveport);
  61. //extern Nlm_Boolean   gDialogTextMultiline;
  62.  
  63.         //fEditSeq
  64. class DAlnSequence : public DAlnTextClass, public DKeyCallback
  65. {
  66. public:
  67.     DSequence* fSeq;
  68.     Boolean  fVisible;
  69.     
  70.     DAlnSequence(long id, DView* itsSuperior):
  71.         DAlnTextClass(id, itsSuperior, NULL, 1, gSeqFont), // DHiddenText
  72.         //DAlnTextClass(id, itsSuperior, 1, 1, gSeqFont, false), // DDialogScrollText
  73.         fSeq(NULL),fVisible(false)
  74.         {
  75.             SetMultilineText(false);
  76.         }
  77.   virtual void Scroll(Boolean vertical, DView* scrollee, short newval, short oldval);        
  78.     virtual void ShowEdit();
  79.     virtual void HideEdit();
  80.   virtual void ProcessKey( char c);
  81.     virtual void selectAction();
  82.     virtual void deselectAction();
  83. };
  84.  
  85. void DAlnSequence::Scroll(Boolean vertical, DView* scrollee, short newval, short oldval) 
  86. {
  87.     if (fVisible && !vertical) { 
  88.         Nlm_HScrollText( NULL, (Nlm_GraphiC)GetNlmObject(), newval, oldval);
  89.         }
  90. }
  91.  
  92. void DAlnSequence::ShowEdit()
  93. {
  94.     if (fSeq) SetText( fSeq->Bases());
  95.     if (!fVisible) {
  96.         fVisible= true;
  97.         Show(); 
  98.         Enable();
  99.         }
  100.         // may need this for MSWIN
  101.     //Nlm_DoActivate(GetNlmObject(),false); 
  102. }
  103.  
  104. void DAlnSequence::HideEdit()
  105. {
  106.     if (fVisible) {
  107.         fVisible= false;
  108.         Disable();
  109.         Hide();
  110.         }
  111. }
  112.  
  113. void DAlnSequence::selectAction()
  114. {
  115.     DAlnTextClass::selectAction();
  116.     //gCursor->ibeam();
  117.     SetKeyHandler(this); //?? also need this call in a window activate/deactivate method??
  118. }
  119.  
  120. void DAlnSequence::deselectAction()
  121. {
  122.     SetKeyHandler(NULL); //?? also need this call in a window activate/deactivate method??
  123.     //fDoc->SetEditText(NULL);
  124.     //gCursor->arrow();
  125.     DAlnTextClass::deselectAction();
  126. }
  127.  
  128. void DAlnSequence::ProcessKey( char c)
  129. {
  130.     enum moves {
  131.         down1 =  1,
  132.         up1        = -1,
  133.         downPage= 10,  // this is arbitrary...
  134.         upPage  = -10,
  135.         toBottom=  25000,
  136.         toTop        = -25000
  137.         };
  138.         
  139.     if ( IsEnabled()) { 
  140.         // dang vibrant doens't have means to change this key, and default handlers still get
  141.         // a crack at it...
  142.  
  143.         switch (c) {
  144.         
  145. #if 0
  146.             case chTab: 
  147.               //? do horizontal shift here ?
  148.                 break;
  149.  
  150.             case chRight:
  151.                 if (gKeys->shift()) ;
  152.                 else ;
  153.             case chLeft:
  154.                 if (gKeys->shift()) ;
  155.                 else ;
  156.                 
  157.             case chUp:
  158.                 if (gKeys->shift()) vertMove(toTop);
  159.                 else vertMove(up1);
  160.                 break;
  161.                 
  162.             case chDown:
  163.             case chEnter:
  164.             case chReturn:
  165.                 if (gKeys->shift())  vertMove(toBottom);
  166.                 else vertMove(down1);
  167.                 break;
  168.                 
  169.             case chPageDown    :    vertMove( downPage); break;
  170.             case chPageUp        :    vertMove( upPage); break;
  171.             case chHome            : vertMove( toTop); break;
  172.             case chEnd             : vertMove( toBottom); break;    
  173. #endif
  174.         
  175.             default: 
  176.                 if (!fSeq->GoodChar(c)) Nlm_Beep(); 
  177.                 break; 
  178.             }
  179.         }
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. class DMaskSelector : public DTabSelector 
  187. {
  188. public:
  189.     enum { cMaskSelector = 20732 };
  190.     short fMaskLevel;
  191.     DSeqList* fSeqList;
  192.     
  193.     DMaskSelector( DTableView* itsTable, short masklevel, DSeqList* seqlist) : 
  194.         DTabSelector( itsTable),
  195.         fSeqList( seqlist),  fMaskLevel(masklevel) 
  196.         {
  197.         fNumber= cMaskSelector;
  198.         }
  199.     virtual void DoItWork(); 
  200. };
  201.  
  202.  
  203. void DMaskSelector::DoItWork()  
  204. {
  205.     if (fMovedOnce || fDoExtend) {
  206. #if MASKS
  207.             // select masks... instead of, or as well as, table sel rect??
  208.         if (fMaskLevel>0) {
  209.             short row, col, top, bot, left, right;
  210.             Nlm_RecT viewr;
  211.             top= fNewSelection.top;
  212.             bot= fNewSelection.bottom;
  213.             left= fNewSelection.left;
  214.             right= fNewSelection.right;
  215.             for (row= top; row<bot; row++) {
  216.                 DSequence* sq= fSeqList->SeqAt(row);
  217.                 if (sq) {
  218.                     for (col= left; col<right; col++) 
  219.                       if (fDoExtend) sq->SetMaskAt(col,fMaskLevel);
  220.                         else sq->FlipMaskAt(col, fMaskLevel);
  221.                     }                
  222.                 }
  223.             ((DTableView*)fView)->GetCellRect(fNewSelection,viewr);
  224.             ((DTableView*)fView)->InvalRect( viewr);
  225.             }
  226.         else
  227. #endif
  228.         ((DTableView*)fView)->SelectCells( fNewSelection, 
  229.                 fDoExtend, DTableView::kHighlight, DTableView::kSelect);
  230.         }
  231. }
  232.  
  233.  
  234.  
  235.  
  236. //class DAlnView : public DTableView  
  237.  
  238.     // test var for fEditSeq bomb...
  239. DAlnView* gLastEditView = NULL;
  240.  
  241.  
  242. DAlnView::DAlnView( long id, DView* itsSuper, DSeqDoc* itsDocument, DSeqList* itsSeqList, 
  243.                                 long pixwidth, long pixheight) :
  244.     DTableView( id, itsSuper, pixwidth, pixheight, 0, 0, Nlm_stdCharWidth, 2+Nlm_stdFontHeight,
  245.                         true, true),
  246.     fDoc(itsDocument), fSeqList(itsSeqList), fSlider(NULL),
  247.     fEditRow(-1), fMaskLevel(0), fCurSeq(NULL), fEditSeq(NULL),
  248.     fLocked(false), fOwnSeqlist(false)
  249.     fKind= kindAlnView;
  250.     gAlnCharWidth= Nlm_stdCharWidth; //Nlm_CharWidth('G');   
  251.     gAlnCharHeight= Nlm_stdLineHeight;
  252.     this->SetResize( DView::relsuper, DView::relsuper);
  253.     this->SetTableFont(gSeqFont);
  254.     //fSlider= new DAlnSlider( fDoc, this, 0);
  255. }
  256.  
  257.  
  258. DAlnIndex::DAlnIndex( long id, DView* itsSuper, DSeqDoc* itsDocument, DSeqList* itsSeqList,
  259.                  long pixwidth, long pixheight) :
  260.     DTableView( id, itsSuper, pixwidth, pixheight, 0, 4,
  261.             5*Nlm_stdCharWidth, 2+Nlm_stdFontHeight, false, true),
  262.     fNameWidth(15),
  263.     fSizeWidth(8),
  264.     fKindWidth(8),
  265.     fDoc(itsDocument),
  266.     fSeqList(itsSeqList)
  267.     //this->SetSlateBorder(false);
  268.     this->SetResize( DView::fixed, DView::relsuper);
  269.     this->SetTableFont(gTextFont);
  270. }
  271.  
  272. DAlnITitle::DAlnITitle( long id, DView* itsSuper, long pixwidth, long pixheight) :
  273.     DTableView( id, itsSuper, pixwidth, pixheight, 1, 4,
  274.             5*Nlm_stdCharWidth, 2+Nlm_stdFontHeight, false, false),
  275.     fNameWidth(15),
  276.     fSizeWidth(8),
  277.     fKindWidth(8)
  278.     //this->SetSlateBorder(false);
  279.     this->SetResize( DView::fixed, DView::fixed);
  280.     this->SetTableFont(gTextFont);
  281. }
  282.  
  283. DAlnHIndex::DAlnHIndex( long id, DView* itsSuper, DSeqDoc* itsDocument, DSeqList* itsSeqList, long pixwidth, long pixheight) :
  284.     DPanel( id, itsSuper, pixwidth, pixheight, DPanel::simple),
  285.     fDoc(itsDocument),
  286.     fSeqList(itsSeqList)
  287.     //this->SetResize( DView::matchsuper, DView::fixed);
  288.     this->SetResize( DView::relsuper, DView::fixed);
  289. }
  290.  
  291. DAlnView::~DAlnView()
  292. {
  293.     //fSeqList= NULL; // fSeqList is owned by Doc !
  294.     DeInstallEditSeq();
  295.     //if (fSlider) delete fSlider;
  296. #if FIX_LATER
  297.     itsEditSeq= fEditSeq;     
  298.     if (itsEditSeq && itsEditSeq->fSuperior)
  299.         itsEditSeq->fSuperior->RemoveSubView(itsEditSeq);
  300.     delete itsEditSeq;
  301. #endif
  302. }
  303.  
  304.  
  305. void DAlnHIndex::Resize(DView* superview, Nlm_PoinT sizechange)
  306. {
  307.     DPanel::Resize(superview, sizechange);
  308. }
  309.  
  310. void DAlnView::Resize(DView* superview, Nlm_PoinT sizechange)
  311. {
  312.     DTableView::Resize(superview, sizechange);
  313. }
  314.  
  315. void DAlnView::GetReadyToShow()
  316. {
  317.     // assume port is set??
  318.     SelectFont();  
  319.     gAlnCharWidth = Nlm_CharWidth('G');   
  320.     gAlnCharHeight= Nlm_LineHeight() + 2; // Nlm_FontHeight()
  321.     SetItemWidth(0, GetMaxCols(), gAlnCharWidth);
  322.     SetItemHeight(0, GetMaxRows(), gAlnCharHeight);
  323.     this->UpdateAllWidths(); 
  324.     this->UpdateSize();  
  325. }
  326.  
  327. void DAlnIndex::GetReadyToShow()
  328. {
  329.     //SelectFont(); //Nlm_SelectFont(fFont);  
  330.     SetItemWidth(0, GetMaxCols(), fViewrect.right-fViewrect.left-1); //<< width of view
  331.     SetItemHeight(0, GetMaxRows(), gAlnCharHeight);
  332. }
  333.  
  334. void DAlnITitle::GetReadyToShow()
  335. {
  336.     // assume port is set??
  337.     SelectFont();
  338.     short charheight=  Nlm_LineHeight(); // Nlm_FontHeight();
  339.     SetItemWidth(0, GetMaxCols(), fViewrect.right-fViewrect.left-1); //<< width of view
  340.     SetItemHeight(0, GetMaxRows(), charheight);
  341. }
  342.  
  343. void DAlnView::Show()
  344. {
  345.     GetReadyToShow();
  346.     DTableView::Show();
  347. }
  348.  
  349. void DAlnIndex::Show()
  350. {
  351.     GetReadyToShow();
  352.     DTableView::Show();
  353. }
  354.  
  355. void DAlnView::Drag(Nlm_PoinT mouse)
  356. {
  357.     DTableView::Drag(mouse); // sets fMouseStillDown= true
  358. }
  359.  
  360. void DAlnIndex::Drag(Nlm_PoinT mouse)
  361. {
  362.     DTableView::Drag(mouse); // sets fMouseStillDown= true
  363. }
  364.  
  365. void DAlnHIndex::Drag(Nlm_PoinT mouse)
  366. {
  367.     DPanel::Drag(mouse);
  368. }
  369.  
  370. void DAlnView::Hold(Nlm_PoinT mouse)
  371. {
  372.     DTableView::Hold(mouse);
  373. }
  374.  
  375. void DAlnIndex::Hold(Nlm_PoinT mouse)
  376. {
  377.     DTableView::Hold(mouse);
  378. }
  379.  
  380. void DAlnHIndex::Hold(Nlm_PoinT mouse)
  381. {
  382.     DPanel::Hold(mouse);
  383. }
  384.  
  385. void DAlnView::Release(Nlm_PoinT mouse)
  386. {
  387.     DTableView::Release(mouse);
  388. }
  389.  
  390. void DAlnIndex::Release(Nlm_PoinT mouse)
  391. {
  392.     DTableView::Release(mouse);
  393. }
  394.  
  395. void DAlnHIndex::Release(Nlm_PoinT mouse)
  396. {
  397.     DPanel::Release(mouse);
  398. }
  399.  
  400.  
  401. void DAlnView::DoubleClickAt(short row, short col)
  402. {
  403. #if 0
  404.     //?? do this only on DAlnIndex dblclik??
  405.     DSequence* ag= fSeqList->SeqAt(row); 
  406.     if (ag) fDoc->OpenSeqedWindow(ag);
  407. #endif
  408. }
  409.  
  410.  
  411. void DAlnIndex::Click(Nlm_PoinT mouse)
  412. {
  413.     short      row, col;
  414.     //if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  415.     
  416.     fDoc->fAlnView->DeInstallEditSeq();
  417.     fDoc->fAlnView->SetEmptySelection(true); 
  418.  
  419.     PointToCell( mouse, row, col);
  420.     if (Nlm_dblClick)
  421.         ;
  422.     else if (IsSelected(row, col)) {   //if (gKeys->shift()) 
  423.         // slide/shift selected lines...
  424.         DAlnShifter* shifter= new DAlnShifter();  
  425.         shifter->IAlnSlider( fDoc, this, fDoc->fAlnView, row);
  426.         fCurrentTracker= shifter;
  427.         }
  428.     else {
  429.         //if (fTabSelector) fTabSelector->suicide();  // is this delete causing bombs? YES, in motif
  430.         fTabSelector= new DTabSelector( this);
  431.         fCurrentTracker= fTabSelector;
  432.      }
  433.     DTableView::Click( mouse);
  434. }
  435.  
  436.  
  437. void DAlnITitle::Click(Nlm_PoinT mouse)
  438. {
  439.         // damn, need this or the like for each user of DTableView::Click ...
  440.     //if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  441.  
  442.     //if (fTabSelector) fTabSelector->suicide();  // is this delete causing bombs? YES, in motif
  443.     fTabSelector= new DTabSelector( this);
  444.     fCurrentTracker= fTabSelector;
  445.  
  446.     DTableView::Click( mouse);
  447. }
  448.  
  449.  
  450.  
  451.  
  452.  
  453. void DAlnView::Click(Nlm_PoinT mouse)
  454. {
  455.     short     row, col;
  456.  
  457.     //if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  458.  
  459.     PointToCell( mouse, row, col);
  460.     if (IsSelected(row, col)) {
  461.         //if (fSlider) delete fSlider;  // is this delete causing bombs?
  462.         fSlider= new DAlnSlider();
  463.         fSlider->IAlnSlider( fDoc, this, this, 0);
  464.         fCurrentTracker= fSlider;
  465.         }
  466.     else {
  467.         //if (fTabSelector) fTabSelector->suicide();  // is this delete causing bombs? YES, in motif
  468. #if MASKS
  469.         if (fMaskLevel>0) 
  470.             fTabSelector= new DMaskSelector( this, fMaskLevel, fSeqList);
  471.         else
  472. #endif
  473.             fTabSelector= new DTabSelector( this);
  474.         fCurrentTracker= fTabSelector;
  475.         }
  476.  
  477.     DTableView::Click( mouse);
  478. }
  479.  
  480.  
  481. void DAlnView::TrackMouse( short aTrackPhase,
  482.                     Nlm_PoinT& anchorPoint, Nlm_PoinT& previousPoint,
  483.                     Nlm_PoinT& nextPoint,    Nlm_Boolean mouseDidMove)
  484. {
  485.     DTableView::TrackMouse( aTrackPhase, anchorPoint, previousPoint, nextPoint,mouseDidMove); 
  486. }
  487.  
  488.  
  489. void DAlnView::TrackFeedback( short aTrackPhase,
  490.                     const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  491.                     const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  492. {
  493.     DTableView::TrackFeedback( aTrackPhase, anchorPoint, previousPoint, nextPoint,
  494.                     mouseDidMove, turnItOn);
  495. }
  496.  
  497.  
  498. void DAlnIndex::DoubleClickAt(short row, short col)
  499. {
  500.     DSequence* ag= fSeqList->SeqAt(row); 
  501.     if (ag) fDoc->OpenSeqedWindow(ag);
  502. }
  503.  
  504. void DAlnITitle::DoubleClickAt(short row, short col)
  505. {
  506. }
  507.  
  508.  
  509.  
  510. void DAlnView::SingleClickAt(short row, short col)
  511. {
  512. #if ETEXT
  513.     if (!(fLocked || gKeys->shift() || gKeys->command() || gKeys->option() )) {  
  514.         SetEmptySelection( true); 
  515.         if (fDoc->fAlnIndex) fDoc->fAlnIndex->SetEmptySelection( true); 
  516.         InstallEditSeq(row,col,col,false);
  517.         }
  518.     else
  519. #endif
  520. #if MASKS
  521.     if (fMaskLevel>0) {
  522.         Nlm_RecT r;
  523.         DSequence* ag= fSeqList->SeqAt(row); 
  524.         if (ag) {
  525.             if (gKeys->shift()) ag->SetMaskAt(col,fMaskLevel);
  526.             else ag->FlipMaskAt(col,fMaskLevel);
  527.             }
  528.         GetCellRect( row, col, r);
  529.         InvalRect( r);
  530.         }
  531.     else
  532. #endif
  533.          DTableView::SingleClickAt(row,  col);
  534.      //fDoc->SetViewMenu();
  535. }
  536.  
  537. void DAlnIndex::SingleClickAt(short row, short col)
  538. {
  539.      DTableView::SingleClickAt(row,  col);
  540.      //fDoc->SetViewMenu();
  541. }
  542.  
  543. void DAlnITitle::SingleClickAt(short row, short col)
  544. {
  545. }
  546.  
  547. void DAlnHIndex::Click(Nlm_PoinT mouse)
  548. {
  549.     DPanel::Click(mouse);
  550. }
  551.  
  552.  
  553. void DAlnView::Scroll(Boolean vertical, DView* scrollee, short newval, short oldval)
  554. {
  555.     short diff= newval - oldval;
  556.     DTableView::Scroll(vertical, scrollee, newval, oldval);
  557.     
  558.     if (diff && fEditSeq && fEditSeq->fVisible) {
  559.         Nlm_RecT    r;
  560.         short delta;
  561.         if (vertical) {
  562.             delta= diff * fItemHeight; // bad for variable line height
  563.             fEditSeq->GetPosition( r);
  564.             Nlm_OffsetRect( &r, 0, -delta);
  565.             if (r.top < fRect.top || r.top >= fRect.bottom) DeInstallEditSeq();
  566.             else fEditSeq->SetPosition( r);
  567.             }
  568.         else {
  569.                 // messy, undo bitmap scroll by TableView for just editseq rect...
  570.             delta= diff * fItemWidth;
  571.             fEditSeq->GetPosition(r);
  572.             Nlm_ScrollRect(&r, delta, 0);
  573.                     // then redo scroll using edit method !
  574.             fEditSeq->Scroll(vertical, scrollee, newval, oldval);
  575.             }
  576.         }
  577.         
  578.     if (scrollee == this) {
  579.         if (vertical) {
  580.             fDoc->fAlnIndex->Scroll(vertical, scrollee, newval, oldval);
  581.             }
  582.         else {
  583.             fDoc->fAlnHIndex->Scroll(vertical, scrollee, newval, oldval);
  584.             }
  585.         }
  586. }
  587.  
  588. void DAlnIndex::Scroll(Boolean vertical, DView* scrollee, short newval, short oldval)
  589. {
  590.     DTableView::Scroll(vertical, scrollee, newval, oldval);
  591.     if (scrollee == this) {
  592.         if (vertical) {
  593.             fDoc->fAlnView->Scroll(vertical, scrollee, newval, oldval);
  594.             }
  595.         else   // horizontal
  596.             fDoc->fAlnITitle->Scroll(vertical, scrollee, newval, oldval);
  597.              
  598.         }
  599. }
  600.  
  601.  
  602. void DAlnHIndex::Scroll(Boolean vertical, DView* scrollee, short newval, short oldval)
  603. {
  604.     Nlm_RecT    r;
  605.     short delta;
  606.     short diff= newval-oldval;
  607.     ViewRect(r);
  608.     if (vertical) {
  609. #if 0
  610.         delta= diff * cHeight;
  611.         Nlm_ScrollRect (&r, 0, -delta);
  612.         if (diff<0) r.bottom= r.top - delta + Min(5,-delta/2);
  613.         else r.top= r.bottom - delta - Min(5,delta/2);
  614. #endif
  615.         }
  616.     else {
  617. #if 1
  618.         short cWidth= gAlnCharWidth;
  619.         delta= diff * cWidth;
  620.         Nlm_ScrollRect (&r, -delta, 0);
  621.             // test fix for off-by-a-few-pixels in scroll...
  622.         if (diff<0) r.right= r.left - delta + Min(5,-delta/2);
  623.         else r.left= r.right - delta - Min(5,delta/2);
  624. #endif
  625.         }
  626.     this->Select(); // need for motif !
  627.     this->InvalRect( r);
  628.  
  629.     if (scrollee == this) {
  630.         if (!vertical) fDoc->fAlnView->Scroll(vertical, scrollee, newval, oldval);
  631.         }
  632. }
  633.  
  634.  
  635.  
  636. DSequence* DAlnView::SelectedSequence(short& selectedRow)
  637. {
  638.     if (GetSelectedRow() != kNoSelection) {
  639.         selectedRow= GetSelectedRow();
  640.         return fSeqList->SeqAt(selectedRow);
  641.         }
  642.     else if (fDoc && fDoc->fAlnIndex->GetSelectedRow() != kNoSelection) {
  643.         selectedRow= fDoc->fAlnIndex->GetSelectedRow();
  644.         return fSeqList->SeqAt(selectedRow);    
  645.         }
  646.     else {
  647.         selectedRow= kNoSelection;
  648.         return NULL;
  649.         }
  650. }
  651.  
  652.  
  653. void DAlnView::UpdateSize()
  654. {    
  655.     long diff;   
  656.     if (fSeqList) diff= fSeqList->GetSize() - GetMaxRows();
  657.     else diff= -GetMaxRows();
  658.     ChangeRowSize( -1, diff);  // -1 prevents redraw..else use fMaxRows    
  659.     if (fDoc && fDoc->fAlnIndex) fDoc->fAlnIndex->ChangeRowSize(-1, diff);
  660.  
  661. #if MASKS
  662.     if (diff>0) { //fMaskLevel>0 && 
  663.         short i, nseq= fSeqList->GetSize();
  664.         for (i=nseq-diff; i<nseq; i++) {
  665.             DSequence* aseq= fSeqList->SeqAt(i);
  666.             if (fMaskLevel>0 || aseq->Masks()) aseq->FixMasks();
  667.             }
  668.         }
  669. #endif
  670. }
  671.  
  672.  
  673. void DAlnView::UpdateWidth(DSequence* aSeq)
  674.     long alnlen= aSeq->LengthF() + 30;
  675.     if (alnlen > GetMaxCols()) ChangeColSize( -1, alnlen-GetMaxCols());  
  676.     SetItemWidth( 0, GetMaxCols(), gAlnCharWidth);  
  677. #if MASKS
  678.     if (fMaskLevel>0 || aSeq->Masks()) aSeq->FixMasks();
  679. #endif
  680.     //if (fDoc && fDoc->fAlnHIndex) fDoc->fAlnHIndex->UpdateWidth(); //keep ruler in sync
  681. }
  682.  
  683. void DAlnView::UpdateAllWidths()
  684. {
  685.     long alnlen= 0;
  686.     short i, nseq= fSeqList->GetSize();
  687.     for (i=0; i<nseq; i++) {
  688.         DSequence* aSeq= fSeqList->SeqAt(i);
  689.         alnlen= Max(alnlen, aSeq->LengthF()); 
  690. #if MASKS
  691.         if (fMaskLevel>0 || aSeq->Masks()) aSeq->FixMasks();
  692. #endif
  693.         }
  694.     alnlen += 30; 
  695.     if (alnlen > GetMaxCols()) ChangeColSize( -1, alnlen-GetMaxCols());
  696.     SetItemWidth( 0, GetMaxCols(), gAlnCharWidth);  
  697.     //if (fDoc && fDoc->fAlnHIndex) fDoc->fAlnHIndex->UpdateWidth(); //keep ruler in sync
  698. }
  699.  
  700. void DAlnView::SetTextLock( Boolean turnon)
  701. {
  702.     fLocked= turnon;
  703.     if (fLocked) DeInstallEditSeq();
  704. }
  705.  
  706.  
  707. void DAlnView::SetViewMode(short viewmode) 
  708. {
  709.     switch (viewmode) {
  710.         case kModeSlide: 
  711.             this->SetTextLock( true);
  712.             if (this->fMaskLevel>0) this->Invalidate();
  713.             this->fMaskLevel= 0; 
  714.             break;
  715.             
  716.         case kModeEdit:  
  717.             this->SetTextLock( false);
  718.             if (this->fMaskLevel>0) this->Invalidate();
  719.             this->fMaskLevel= 0; 
  720.             break;
  721.             
  722.         case kModeMask1: 
  723.         case kModeMask2: 
  724.         case kModeMask3: 
  725.         case kModeMask4: {
  726.             this->SetTextLock( true);
  727.             this->fMaskLevel= viewmode - kModeMask1 + 1; 
  728.             this->Invalidate();
  729.             short i, nseq= fSeqList->GetSize();
  730.             for (i=0; i<nseq; i++) fSeqList->SeqAt(i)->FixMasks();
  731.             }
  732.             break;
  733.             
  734.         default :
  735.             break;
  736.         }
  737. }
  738.  
  739.  
  740. // move this to DSeqCmds.h ...
  741. class DSetEditCmd : public DSeqChangeCmd {
  742. public: 
  743.     DSetEditCmd( DSeqDoc* itsAlnDoc, DView* itsView, DSequence* oldSeq, char* newbases):
  744.          DSeqChangeCmd("edit seq", itsAlnDoc, itsView, NULL) 
  745.          {
  746.             fOldSeqs= new DSeqList(); 
  747.              fOldSeqs->InsertLast( oldSeq);
  748.             DSequence* newSeq= MakeSequence( oldSeq->Name(), newbases, oldSeq->Info(), 0);
  749.             fNewSeqs->InsertLast( newSeq);
  750.          }
  751. };
  752.  
  753.  
  754. void DAlnView::DeInstallEditSeq()
  755. {
  756.     if (fEditRow >= 0) { 
  757.         if (fEditSeq) {
  758.             Nlm_RecT r;
  759.             char* newbases = fEditSeq->GetText();
  760.             if (StringCmp(newbases, fEditSeq->fSeq->Bases()) != 0) {
  761. #if 0
  762.                 // test if DSetEditCmd is cause of bombs when fEditSeq is used...
  763.                 // no -- this isn't it...
  764.                 fEditSeq->fSeq->SetBases(newbases);                    
  765. #else
  766.                 DSetEditCmd* cmd= new DSetEditCmd( fDoc, this, fEditSeq->fSeq, newbases);
  767.                 if (cmd) PostTask(cmd); 
  768. #endif
  769.                 }
  770.             MemFree( newbases);
  771.           
  772.             fEditSeq->GetPosition(r);
  773.             fEditSeq->HideEdit();
  774.             fDoc->SetEditText(NULL); //??
  775.             InvalRect(r);
  776.             }
  777.         fEditRow= -1;
  778.         }
  779.  
  780. }
  781.  
  782.  
  783.  
  784. void DAlnView::InstallEditSeq(short row, short selStart, short selEnd, Boolean doLight)
  785. {
  786.          
  787.     DeInstallEditSeq();
  788.     //if (gLastEditView && gLastEditView != this) gLastEditView->DeInstallEditSeq();
  789.  
  790.     fEditRow= row;
  791.     if (row >= 0) {
  792.         Nlm_RecT r;
  793.         DSequence* aSeq= fSeqList->SeqAt( row);
  794.         this->GetRowRect( row, r);
  795.         fEditSeq->fSeq= aSeq;
  796.  
  797.         r.left--; // fix off-by-one
  798.         fEditSeq->SetPosition( r);
  799.         fEditSeq->ShowEdit();
  800.         fEditSeq->SetSelection( selStart, selEnd);
  801.         if (fLeft>0) fEditSeq->Scroll(false, NULL, fLeft, 0);
  802.         fDoc->SetEditText(fEditSeq); //??
  803.  
  804.         if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  805.         //gLastEditView= this;
  806.         }
  807.  
  808. }
  809.  
  810.  
  811. void DAlnView::registerInsertLast( DSequence* aSeq)
  812. {
  813.     ChangeRowSize( GetMaxRows(), 1);
  814.     if (fDoc && fDoc->fAlnIndex) 
  815.         fDoc->fAlnIndex->ChangeRowSize( GetMaxRows(), 1);
  816.     UpdateWidth(aSeq);    
  817. }
  818.  
  819. void DAlnView::addToAlnList( DSequence* aSeq)
  820. {
  821.     fSeqList->InsertLast( aSeq);
  822.     //aSeq->fIndex= fSeqList->GetSize(); //??
  823.     registerInsertLast( aSeq); 
  824.  
  825. #if NOT_NOW_MASKS
  826.         // done via registerInsertLast -> UpdateWidth
  827.         if (fMaskLevel>0 || aSeq->fMasks) aSeq->FixMasks();
  828. #endif
  829. }
  830.  
  831.  
  832. void DAlnView::MakeConsensus()
  833.     fSeqList->MakeConsensus();
  834.     short arow= fSeqList->ConsensusRow();
  835.     if (arow>0) {
  836.         Nlm_RecT r;
  837.         GetRowRect( arow, r);
  838.         this->InvalRect( r);
  839.         }
  840. }
  841.  
  842.  
  843. char* DAlnView::FindCommonBases( short minCommonPerCent)
  844. {
  845. // THIS_IS_OBSOLETE
  846.     char *hCommon, *hFirst = NULL;
  847.     //gMinCommonPercent= minCommonPerCent;
  848.     hCommon= fSeqList->FindCommonBases(DSeqList::gMinCommonPercent, hFirst);
  849.     if (hFirst) MemFree( hFirst);
  850.     return hCommon;
  851. }
  852.  
  853.  
  854. void DAlnView::HiliteORFs()
  855. {
  856.     long    start, stop;
  857.     long nseq= fSeqList->GetSize();
  858.     if (fMaskLevel>0) 
  859.     for (long iseq= 0; iseq<nseq; iseq++) {
  860.         DSequence* aSeq= fSeqList->SeqAt(iseq);
  861.         long alen= aSeq->LengthF();
  862.         aSeq->ClearMask(fMaskLevel);
  863.         aSeq->ClearSelection();
  864.  
  865.         aSeq->SearchORF( start, stop);
  866.         while (start>=0) {
  867.             if (stop<start) stop= alen;
  868.             for (long ibase=start; ibase<=stop; ibase++) 
  869.                 aSeq->SetMaskAt( ibase, fMaskLevel);
  870.             if (stop >= alen) start= -1;
  871.             else {
  872.               aSeq->SetSelection( stop+1, alen);
  873.                 aSeq->SearchORF( start, stop);
  874.                 }
  875.             }
  876.         } 
  877.     this->Invalidate();
  878. }
  879.  
  880.  
  881. void DAlnView::HiliteCommonBases()
  882. {  
  883. #if 1
  884.     char * hFirst = NULL;
  885.     char * hCommon= fSeqList->FindCommonBases(DSeqList::gMinCommonPercent, hFirst);
  886.     
  887.     long nseq= fSeqList->GetSize();
  888.     if (fMaskLevel>0) 
  889.     for (long iseq= 0; iseq<nseq; iseq++) {
  890.         DSequence* aSeq= fSeqList->SeqAt(iseq);
  891.         long alen= aSeq->LengthF();
  892.         char * hf = hFirst;
  893.         char * hc = hCommon;
  894.         char * bc = aSeq->Bases();
  895.         aSeq->ClearMask(fMaskLevel);
  896.         for (long ibase=0; ibase<alen; ibase++, hf++, hc++, bc++) 
  897.             if (iseq >= (unsigned char) *hf && toupper(*bc) == *hc)
  898.                 aSeq->SetMaskAt( ibase, fMaskLevel);
  899.         }
  900.     this->Invalidate();
  901.     MemFree(hCommon);
  902.     MemFree(hFirst);
  903. #endif
  904.  
  905. #if THIS_IS_OBSOLETE
  906.     char        *hCon, *hSeq, *hFirst, *hMaxbase;
  907.     long        maxlen;
  908.     short     arow;
  909.      
  910.     DSequence* cons= fSeqList->Consensus();
  911.     if (!cons) cons= (DSequence*) fSeqList->First();
  912.  
  913.     if (cons) {
  914.         arow= fSeqList->GetIdentityItemNo( cons);
  915.         hCon= cons->Bases();   
  916.         hMaxbase= fSeqList->FindCommonBases(DSeqList::gMinCommonPercent, hFirst); 
  917.         
  918.         maxlen= StrLen(hMaxbase);
  919.         this->SetEmptySelection( true);        
  920.         long iseq, nseq= fSeqList->GetSize();
  921.         for (iseq= 0; iseq<nseq; iseq++) {
  922.             DSequence* aSeq= fSeqList->SeqAt(iseq);
  923.             if (!aSeq->IsConsensus() && aSeq->Kind() != DSequence::kOtherSeq) {
  924.                 hSeq= aSeq->Bases();
  925.                 for (short ibase=0; ibase<maxlen; ibase++) {
  926.                     if ( toupper(hSeq[ibase]) == hMaxbase[ibase] )  
  927.                       this->SelectCells( iseq, ibase, false, false);  
  928.                     }
  929.                 }
  930.             }
  931.         this->InvalidateSelection();
  932.         MemFree(hMaxbase); 
  933.         MemFree(hFirst); 
  934.         }
  935. #endif
  936. }
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943. void DAlnView::DrawAlnInStyle( baseColors colors, Boolean swapBackColor,
  944.                                                             char*    pText, long indx, long len, short row)
  945. {
  946. #if MASKS
  947.         
  948.         // ?? do we want this? main use of styles is in PrettyPrint...
  949.         
  950.     Nlm_PoinT pt;
  951.     long            endx, i, pend, skip;
  952.     char            ch, lastch;
  953.     Nlm_RecT    crec; // set this to rect about draw char...
  954.     short         maskval;
  955.         
  956.     Boolean dostyles= (fCurSeq && fCurSeq->MasksOk());
  957.     if (!dostyles) {
  958.         DrawAlnColors( colors, swapBackColor, pText, indx, len,row);
  959.         return;
  960.         }
  961.         
  962.     long cw= GetItemWidth(); //gAlnCharWidth; //Nlm_CharWidth('G');
  963.     long cht= GetItemHeight();
  964.     Nlm_GetPen( &pt);
  965.     if (pt.x < 0) {
  966.         skip= (-pt.x) / cw;
  967.       indx += skip;
  968.         len -= skip;
  969.         pt.x += skip*cw;
  970.         Nlm_MoveTo( pt.x, pt.y);
  971.         }
  972.     pend= len * cw;
  973.     
  974.     if (pend > GetRect().right) {   
  975.         skip= (pend - GetRect().right) / cw;  
  976.         len -= skip;
  977.         }
  978.     endx= indx + len - 1;
  979.     lastch= 0;
  980.     
  981.     for (i= indx; i<=endx; i++) {
  982.         ch= pText[i];
  983.         if (ch >= ' ') {
  984.             if (ch!=lastch) Nlm_SetColor( colors[ch-' ']);
  985.             Nlm_PaintChar(ch);
  986.             lastch= ch;
  987.             if (fMaskLevel>0) {
  988.                 maskval= fCurSeq->MaskAt(i, fMaskLevel);
  989.                 if (maskval>0) {
  990.                     Nlm_LoadRect( &crec, pt.x, pt.y-cht+2, pt.x+cw, pt.y+2); //??
  991.                     //Nlm_FrameRect( &crec); //?? doesn't show on XMotif?
  992.                     DTableView::InvertRect( crec); 
  993.                     }
  994.                 }
  995.             pt.x += cw;
  996.             // !? must reset some of drawing environ !? > Font !
  997.             }
  998.         }
  999.     Nlm_Black();
  1000.     
  1001. #else
  1002.     DrawAlnColors( colors, swapBackColor, pText, indx, len, row);
  1003. #endif
  1004. }
  1005.  
  1006.  
  1007. void DAlnView::DrawAlnColors(baseColors colors, Boolean swapBackColor,
  1008.                                                             char*    pText, long indx, long len, short row)
  1009. {
  1010.     Nlm_PoinT pt;
  1011.     long            endx, i, pend, skip;
  1012.     char            ch, lastch;
  1013.     
  1014.     long cw= GetItemWidth(); //gAlnCharWidth; //Nlm_CharWidth('G');
  1015.     Nlm_GetPen( &pt);
  1016.     if (pt.x < 0) {
  1017.         skip= (-pt.x) / cw;
  1018.       indx += skip;
  1019.         len -= skip;
  1020.         Nlm_MoveTo( pt.x + skip*cw, pt.y);
  1021.         }
  1022.     pend= len * cw;
  1023.     
  1024.     if (pend > GetRect().right) {   
  1025.         skip= (pend - GetRect().right) / cw;  
  1026.         len -= skip;
  1027.         }
  1028.     endx= indx + len - 1;
  1029.     
  1030.     //-- backcolor not showing -- need erase or something...
  1031.     //if (swapBackcolor) RGBForeColor( colors[' '-' ']); else RGBBackColor( colors[' '-' ']);
  1032.      
  1033.     //Nlm_CopyMode(); //?? assume?
  1034.     lastch= 0;
  1035.     for (i= indx; i<=endx; i++) {
  1036.         ch= pText[i];
  1037.         if (ch >= ' ') {
  1038.             // if (swapBackcolor) RGBBackColor( colors[ch-' ']); else 
  1039.             if (ch!=lastch) Nlm_SetColor( colors[ch-' ']);
  1040.             Nlm_PaintChar(ch);
  1041.             lastch= ch;
  1042.             }
  1043.         }
  1044.     Nlm_Black();
  1045.  
  1046.  
  1047.  
  1048.  
  1049. void DAlnView::DrawAllColors(Nlm_RecT r, short row)
  1050. #if MASKS
  1051. #define DRAWALN    DrawAlnInStyle
  1052. #else
  1053. #define DRAWALN    DrawAlnColors
  1054. #endif
  1055.  
  1056.     long stopcol, startcol = GetLeft(); 
  1057.     long newright= r.left;
  1058.  
  1059.     for (stopcol= startcol; stopcol<GetMaxCols() && newright<r.right; stopcol++) {
  1060.         //if (fWidths) newright += fWidths[startcol]; else 
  1061.         newright += GetItemWidth();
  1062.         // optimize later -- check update region by each char rect ...
  1063.         //if (Nlm_RectInRgn (&item_rect, Nlm_updateRgn)) done= true;
  1064.         }
  1065.  
  1066.     DSequence* aSeq= fSeqList->SeqAt(row);
  1067.     if (aSeq && aSeq->Bases()) {
  1068.         char* hSeq= aSeq->Bases();
  1069.         fCurSeq= aSeq;
  1070.         long len= Min( aSeq->LengthF(), stopcol) - startcol;
  1071.         if (len>0 && hSeq) {
  1072.             Nlm_MoveTo( r.left, r.bottom-2); //bottom-5
  1073.             if (aSeq->Kind() == DSequence::kAmino)
  1074.                 DRAWALN(DBaseColors::gAAcolors, gSwapBackground, hSeq, startcol, len, row);
  1075.             else
  1076.                 DRAWALN(DBaseColors::gNAcolors, gSwapBackground, hSeq, startcol, len, row);
  1077.             }
  1078.         }
  1079.     fColsDrawn= stopcol - startcol;
  1080. }  
  1081.  
  1082. void DAlnView::DrawNoColors(Nlm_RecT r, short row)
  1083.     long stopcol, startcol = GetLeft(); 
  1084.     long newright= r.left;
  1085.     short    cwidth= GetItemWidth(); // Nlm_CharWidth('G'); //fItemWidth
  1086.     
  1087.     for (stopcol= startcol; stopcol<GetMaxCols() && newright<r.right; stopcol++) {
  1088.         //if (fWidths) newright += fWidths[startcol]; else 
  1089.         newright += cwidth;
  1090.         // optimize later -- check update region by each char rect ...
  1091.         //if (Nlm_RectInRgn (&item_rect, Nlm_updateRgn)) done= true;
  1092.         }
  1093.  
  1094.     DSequence* aSeq= fSeqList->SeqAt(row);
  1095.     if (aSeq && aSeq->Bases()) {
  1096.         char *s, se, *hSeq= aSeq->Bases();
  1097.         long len= Min( aSeq->LengthF(), stopcol) - startcol;
  1098.         if (len>0 && hSeq) {
  1099. #if MASKS
  1100.             if (fMaskLevel>0) {
  1101.                 short atx= r.left;
  1102.                 short aty= r.bottom-2;
  1103.                 stopcol= startcol + len;
  1104.                 for (long i= startcol; i<stopcol; i++) {
  1105.                     Nlm_MoveTo(atx, aty);
  1106.                     Nlm_PaintChar(hSeq[i]);
  1107.                     short maskval= aSeq->MaskAt(i, fMaskLevel);
  1108.                     if (maskval>0) {
  1109.                         Nlm_RecT crec;
  1110.                         Nlm_LoadRect( &crec, atx, r.top, atx+cwidth, r.bottom); 
  1111.                         //Nlm_FrameRect( &crec); //<< ? bad for Motif, also messy looking
  1112.                         DTableView::InvertRect( crec); 
  1113.                         }
  1114.                     atx += cwidth;
  1115.                     }
  1116.                 }
  1117.             else 
  1118. #endif
  1119.                 {
  1120.                 Nlm_MoveTo( r.left, r.bottom-2); //bottom-5
  1121.                 s= hSeq+startcol;
  1122.                 se= s[len];
  1123.                 s[len]= 0;
  1124.                 Nlm_PaintString( s); 
  1125.                 s[len]= se;
  1126.                 //Nlm_DrawText( &r, hSeq+startcol, len, 'l', false); // gray== false
  1127.                 }
  1128.         }
  1129.         }
  1130.     fColsDrawn= stopcol - startcol;
  1131. }  
  1132.  
  1133.         
  1134. void DAlnView::Draw()
  1135. {
  1136.     DTableView::Draw();
  1137. }
  1138.  
  1139.  
  1140. void DAlnView::DrawRow(Nlm_RecT r, short row)
  1141. {
  1142.      // DTableView::Draw() does SelectFont(fFont)
  1143.   if (fEditRow == row) {
  1144.          // do nothing? , fEditSeq handle's redraw ??
  1145.         fColsDrawn= 0;
  1146.          }
  1147.     else if (fDoc && fDoc->fUseColor) 
  1148.         DrawAllColors(r, row); 
  1149.     else
  1150.         DrawNoColors(r, row);
  1151. }
  1152.  
  1153.         
  1154. void DAlnIndex::DrawCell(Nlm_RecT r, short row, short col)
  1155. {
  1156. #if 0
  1157.     long stopcol, startcol = fLeft; 
  1158.     long newright= r.left;
  1159.     short    cwidth= Nlm_CharWidth('G'); //fItemWidth
  1160.     
  1161.     for (stopcol= startcol; stopcol<fMaxCols && newright<r.right; stopcol++) {
  1162.         //if (fWidths) newright += fWidths[startcol]; else 
  1163.         newright += cwidth;
  1164.         // optimize later -- check update region by each char rect ...
  1165.         //if (Nlm_RectInRgn (&item_rect, Nlm_updateRgn)) done= true;
  1166.         }
  1167. #endif
  1168.  
  1169.     // add cols for Kind(), Length(), Origin(), UpdateTime(), Info()(wide col..better in box)
  1170.         
  1171.     DSequence* aSeq= fSeqList->SeqAt(row);
  1172.     if (aSeq) switch (col) {
  1173.         case 0: 
  1174.             {
  1175.             char* hSeq= aSeq->Name();
  1176.             Nlm_DrawString( &r, hSeq, 'l', false);
  1177.             return;
  1178.             }
  1179.         case 1:
  1180.             {
  1181.             char buf[128];
  1182.             sprintf(buf,"%d ", aSeq->LengthF()); // switch to ostrstream !?
  1183.             Nlm_DrawString( &r, buf, 'r', false);
  1184.             return;
  1185.             }
  1186.         case 2:
  1187.             {
  1188.             char* hSeq= aSeq->KindStr();
  1189.             Nlm_DrawString( &r, hSeq, 'l', false);
  1190.             return;
  1191.             }
  1192.         case 3:
  1193.             {
  1194.             char buf[128];
  1195.             sprintf(buf,"%8lx ", aSeq->Checksum()); // switch to ostrstream !?
  1196.             Nlm_DrawString( &r, buf, 'l', false);
  1197.             return;
  1198.             }
  1199.         }
  1200. }
  1201.  
  1202.  
  1203. void DAlnITitle::DrawCell(Nlm_RecT r, short row, short col)
  1204. {
  1205.     switch (col) {
  1206.         case 0: 
  1207.             {
  1208.             Nlm_DrawString( &r, "Name", 'l', false);
  1209.             return;
  1210.             }
  1211.         case 1:
  1212.             {
  1213.             Nlm_DrawString( &r, "Bases", 'l', false);
  1214.             return;
  1215.             }
  1216.         case 2:
  1217.             {
  1218.             Nlm_DrawString( &r, "Kind", 'l', false);
  1219.             return;
  1220.             }
  1221.         case 3:
  1222.             {
  1223.             Nlm_DrawString( &r, "Checksum", 'l', false);
  1224.             return;
  1225.             }
  1226.         }
  1227. }
  1228.  
  1229. void DAlnHIndex::Draw()
  1230. {
  1231.     Nlm_RecT r;
  1232.     short atx, aty;
  1233.     char nums[128];
  1234.  
  1235.     fDoc->fAlnView->SelectFont();  
  1236.     ViewRect( r);
  1237.     long stopcol, startcol = fDoc->fAlnView->GetLeft(); 
  1238.     long newright= r.left;
  1239.     short    cwidth= Nlm_CharWidth('G');  
  1240.  
  1241. #if 0    
  1242.     Nlm_RecT vr;
  1243.     // is ViewRect bad !? (too short)  YES - isn't grown !?
  1244.   fDoc->fAlnView->ViewRect( vr);
  1245.   r.right= vr.right; //??
  1246.     Nlm_ClipRect( &r); // ?? why is hindex clipped to small !?
  1247. #endif
  1248.     
  1249.     for (stopcol= startcol; 
  1250.         stopcol < fDoc->fAlnView->GetMaxCols() && newright<r.right;
  1251.       stopcol++) {
  1252.         newright += cwidth;
  1253.         }
  1254.  
  1255.     long len= stopcol - startcol;
  1256.     atx= r.left;
  1257.     aty= r.bottom;
  1258.     for (short i= startcol; i<=stopcol; i++) {
  1259.         if (i == 0) ; //skip
  1260.         else if (i % 10 == 0) {
  1261.             Nlm_MoveTo(atx, aty);
  1262.             Nlm_LineTo(atx, aty-2);
  1263.             sprintf( nums, "%d", i);
  1264.             short ws= Nlm_StringWidth(nums);
  1265.             Nlm_MoveTo( atx-(ws/ 2), aty-3);
  1266.             Nlm_PaintString(nums);
  1267.             //Nlm_DrawString( &r1, nums, 'c', false);
  1268.             }    
  1269.         else if (i % 5 == 0) {
  1270.             Nlm_MoveTo( atx, aty);
  1271.             Nlm_LineTo(atx, aty-6);
  1272.             }
  1273.         atx += cwidth;
  1274.         }
  1275. }
  1276.  
  1277.  
  1278.  
  1279.  
  1280.         
  1281.  
  1282. #if FIX_LATER
  1283. Boolean DAlnView::ContainsClipType(long aType) // override 
  1284. {
  1285.     return (aType == kAlnScrapType);
  1286.     //!? also (aType == 'TEXT') can be pasted as seq... w/ SeqReadScrap...
  1287. }
  1288. #endif
  1289.  
  1290. #if FIX_LATER
  1291. void DAlnView::WriteToDeskScrap() // override 
  1292. {
  1293.     short format = kGenBank;
  1294.     //if (fDoc->fFormatPop) format= fDoc->fFormatPop->GetValue(); 
  1295.     fSeqList->ClearSelections();
  1296.     char* textScrap= fSeqList->doWriteHandle( format);  
  1297.     short err = gClipboardMgr->PutDeskScrapData('TEXT', textScrap);
  1298.     MemFree( textScrap);
  1299. }
  1300. #endif
  1301.  
  1302.  
  1303.  
  1304.  
  1305.         
  1306.         
  1307.         
  1308. // DAlnIndex     ------------------
  1309.  
  1310.  
  1311. char* DAlnIndex::GetItemTitle(short item, char* title, size_t maxsize)   
  1312. {
  1313.   if (fSeqList && item <= fSeqList->GetSize()) {
  1314.         DSequence* aSeq= fSeqList->SeqAt(item);
  1315.         if (aSeq) {
  1316.             if (!title) title= (char*) MemNew( maxsize);
  1317.             StrNCpy( title, aSeq->Name(), maxsize);
  1318.             return title;
  1319.             }
  1320.         }
  1321.     return NULL;
  1322. }
  1323.  
  1324.  
  1325. #if FIX_LATER
  1326.  
  1327. void TAlnIndex::UpdateSize(void)
  1328. VAR    diff: integer;
  1329. {
  1330.     if ((fAlnView!=NULL)) {
  1331.         diff= fAlnView->fNumOfRows - fNumOfRows;
  1332.         if ((diff>0)) InsRowLast( 1, fAlnView->fRowheight)     
  1333.         else if ((diff<0)) DelItemLast( -diff);
  1334.         }    else
  1335.         DelItemLast(fNumOfRows);
  1336. }
  1337.  
  1338. void TAlnIndex::ReSelect(RgnHandle indexRegion)
  1339. VAR  aCell: GridCell;
  1340. {
  1341.     /*-----
  1342.     aCell = (*indexRegion)->rgnBBox.topLeft;
  1343.     if (!IsCellVisible(aCell)) PositionAtCell(aCell);                 
  1344.     ----*/
  1345.     
  1346.     if (!EqualRect((*indexRegion)->rgnBBox, (*fSelections)->rgnBBox)) {
  1347.         /*---- ??
  1348.         WITH fDoc){
  1349.             fAlnView->SetEmptySelection(kHighlight);  
  1350.             }
  1351.         -----*/
  1352.         SetSelection(indexRegion, kDontExtend, kHighlight, kSelect);
  1353.         }
  1354. }
  1355.  
  1356.  
  1357.  
  1358. TAlnIndex::HandleMouseDown( VPoint theMouse, TToolboxEvent event, 
  1359.                              hysteresis: Point):Boolean; // override 
  1360. {        
  1361.     fAlnView->DeInstallEditSeq();
  1362.     fAlnView->SetEmptySelection(kHighlight); 
  1363.     
  1364.   HandleMouseDown= inherited::HandleMouseDown(theMouse,event,hysteresis);
  1365. }
  1366.  
  1367. void TAlnIndex::DoMouseCommand(VPoint VAR theMouse, TToolboxEvent event,
  1368.                                                Point hysteresis) // override 
  1369. VAR
  1370.     aRow, aCol    : short;
  1371.     aCell    : GridCell;
  1372.     aAlnShifter        : TAlnShifter;
  1373.     boolean        aDoubleClick;
  1374.     aName    : Str255;
  1375.     vpt        : VPoint;
  1376.     GridViewPart        gridPart;
  1377.     aSeq    : DSequence;
  1378.     
  1379. {
  1380.     aDoubleClick= event->fClickCount > 1;
  1381.     gridPart= IdentifyPoint(theMouse, aCell);
  1382.     aRow= aCell.y;
  1383.     
  1384.     if (aDoubleClick) {
  1385.         aSeq= DSequence(fSeqList->At(aRow));
  1386.         if (aSeq!=NULL)) TAlnDoc(fDocument)->OpenSeqedWindow(aSeq);
  1387.         }        
  1388.     else if ((event.IsShiftKeyPressed |event->IsCommandKeyPressed())) { 
  1389.         inherited::DoMouseCommand(theMouse,event,hysteresis) 
  1390.         }        
  1391.     else if (gridPart != badChoice) {
  1392.         aCell    = VPointToLastCell(theMouse);
  1393.         //if TRUE then begin
  1394.         if (isCellSelected(aCell)) {
  1395.             GetItemText(aRow, aName);
  1396.             New(aAlnShifter);
  1397.             FailNIL(aAlnShifter);
  1398.             aAlnShifter->IAlnShifter(this, fAlnView, aRow, aName);
  1399.             PostCommand( aAlnShifter);
  1400.             }        else
  1401.             inherited::DoMouseCommand(theMouse,event,hysteresis); 
  1402.         }
  1403. }
  1404.  
  1405. #endif // FIX_LATER
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416. class DSeqFormatPopup : public DPopupList
  1417. {
  1418. public:
  1419.     DSeqFormatPopup(long id, DView* superior, short defaultFormat);
  1420.     virtual short GetValue() { return DSeqFile::kMinFormat + DPopupList::GetValue() - 1; }
  1421. };
  1422.  
  1423. DSeqFormatPopup::DSeqFormatPopup(long id, DView* superior, short defaultFormat) :
  1424.             DPopupList(id,superior,true)
  1425. {
  1426.     for (short i= DSeqFile::kMinFormat; i<=DSeqFile::kMaxFormat; i++) 
  1427.         this->AddItem( (char*)DSeqFile::FormatName(i));
  1428.     this->SetValue(defaultFormat);
  1429. }
  1430.  
  1431.  
  1432.  
  1433. class DAlnModePopup : public DPopupList
  1434. {
  1435. public:
  1436.     DAlnModePopup(long id, DView* superior, short defaultvalue);
  1437.     virtual short GetValue() { return DAlnView::kModeSlide + DPopupList::GetValue() - 1; }
  1438. };
  1439.  
  1440. DAlnModePopup::DAlnModePopup(long id, DView* superior, short defaultvalue) :
  1441.             DPopupList(id,superior,true)
  1442. {
  1443.     AddItem( "Slide bases");
  1444.     AddItem( "Edit bases");
  1445.     AddItem( "Select mask 1");
  1446.     AddItem( "Select mask 2");
  1447.     AddItem( "Select mask 3");
  1448.     AddItem( "Select mask 4");
  1449.     this->SetValue(defaultvalue+1);
  1450. }
  1451.  
  1452.  
  1453.  
  1454. // class DAlnFindDialog
  1455.  
  1456. class DAlnFindDialog : public DFindDialog {
  1457. public:
  1458.     DSequence* fSequence;
  1459.     DAlnView* fAlnView;
  1460.     short     fRow;
  1461.     char    * fLastTarget;
  1462.     long        fLastMatch;
  1463.     DAlnFindDialog( DSequence* itsSeq, DAlnView* itsView);
  1464.     virtual void DoFind();
  1465.     virtual void BuildDlog();
  1466. };
  1467.  
  1468. DAlnFindDialog::DAlnFindDialog( DSequence* itsSeq, DAlnView* itsView) :
  1469.         fSequence(itsSeq), fAlnView( itsView), 
  1470.         fLastTarget(NULL), fRow(-1), fLastMatch(-1)
  1471. {
  1472. }
  1473.  
  1474. void DAlnFindDialog::BuildDlog()
  1475. {
  1476.     DFindDialog::BuildDlog();
  1477.  
  1478.     DView* av;
  1479.     av= FindSubview(replaceId); if (av) { av->Disable(); } //av->Hide();
  1480.     av= FindSubview(replaceFindId); if (av) { av->Disable();  }
  1481.     av= FindSubview(replaceAllId); if (av) { av->Disable(); }
  1482.     av= FindSubview(cBackwards);     if (av) { av->Disable(); }
  1483.     av= FindSubview(cFullWord); if (av) { av->Disable(); }
  1484.     av= FindSubview(cCaseSense); if (av) { av->Disable(); }
  1485.     if (fReplaceText) fReplaceText->Disable();
  1486. }
  1487.  
  1488. void DAlnFindDialog::DoFind() 
  1489. {
  1490.     long    firstBase, nBases, match;
  1491.     const char    *target;
  1492.     Boolean back;
  1493.     Nlm_RecT selr;
  1494.     short        selrow;
  1495.     
  1496.     fSequence= fAlnView->SelectedSequence(selrow);
  1497.     fRow= selrow;
  1498.     if (fSequence) {
  1499.         fSequence->UpdateFlds();  
  1500.         selr= fAlnView->GetSelRect();
  1501.         selr.top= fRow;
  1502.         selr.bottom= fRow+1; //??
  1503.         firstBase= selr.left;
  1504.         nBases= 0; //selr.right - firstBase;
  1505.         fSequence->SetSelection( firstBase, nBases);
  1506.         target= this->GetFind();
  1507.         back    = this->Backwards();
  1508.         if (target && *target) {
  1509.             if (fLastTarget && (StringCmp(target, fLastTarget)==0) && firstBase == fLastMatch)
  1510.                 match= fSequence->SearchAgain();
  1511.             else {
  1512.                 if (fLastTarget) MemFree(fLastTarget);
  1513.                 fLastTarget= StrDup(target);
  1514.                 match= fSequence->Search( (char*)target, back);
  1515.                 }
  1516.             fLastMatch= match;
  1517.             if (match>=0) {
  1518.                 nBases= StrLen(target);
  1519.                 fSequence->SetSelection( match, nBases);
  1520.                 selr.left= match;
  1521.                 selr.right= match + nBases;
  1522.                 fAlnView->SelectCells( selr); // !? this is bad
  1523.                 //fAlnView->InvalidateSelection();
  1524.                 }
  1525.             }
  1526.         }
  1527. }                
  1528.  
  1529.  
  1530.  
  1531. //class DSeqDoc
  1532.  
  1533.  
  1534. DSeqDoc::DSeqDoc( long id, DSeqList* itsSeqList, char* name) :
  1535.         DWindow( id, gApplication),
  1536.         fAlnView(NULL), fAlnIndex(NULL), fAlnITitle(NULL),
  1537.         fSeqList(itsSeqList), fPrintDoc(NULL),
  1538.         fHeadline(NULL), fFormatPop(NULL),
  1539.         fUpdateTime(0), fDocTitle(NULL),
  1540.         fInFormat(DSeqFile::kGenBank), // kNoFormat
  1541.         fSaveSelection(false),
  1542.         //fLockButton(NULL), fColorButton(NULL), fMonoButton(NULL),
  1543.         fColorCheck(NULL), fLockCheck(NULL),
  1544.         fUseColor(false)
  1545.     short width= -1, height= -1, left= -10, top= -20; // default window loc
  1546.     if (gTextFont == NULL) gTextFont= Nlm_programFont;
  1547.     if (gSeqFont == NULL) gSeqFont= Nlm_programFont;
  1548.  
  1549.     if (!Nlm_EmptyRect(&fgWinRect))  {
  1550.             // these two are causing resize to fail >> width & height setting !!
  1551.         //width= MAX( 40, fgWinRect.right - fgWinRect.left);
  1552.         //height= MAX( 50, fgWinRect.bottom - fgWinRect.top);
  1553.         left= MAX(20,fgWinRect.left);
  1554.         top = MAX(40,fgWinRect.top);
  1555.         }
  1556.  
  1557.     this->InitWindow( document, width, height, left, top, name); 
  1558.     gDefSeqName= name; 
  1559.     
  1560.     if (!fSeqList) fSeqList= new DSeqList();  
  1561.     //fSeqList->AddNewSeq();    // install 1 blank seq for new doc
  1562. }
  1563.  
  1564.  
  1565. DSeqDoc::~DSeqDoc()
  1566. {
  1567. #if 0
  1568.         // done now as part of ~DTaskMaster !
  1569.     if (gLastCommand) { 
  1570.     
  1571.         // !! MUST delete any tasks/commands owned by window in task queue !!
  1572.         // at least any 'edit seq' tasks...
  1573.  
  1574.       if (((DView*)gLastCommand->fSource)->GetWindow() == this) {
  1575.             delete gLastCommand; gLastCommand= NULL; 
  1576.             }  
  1577.         }
  1578. #endif
  1579.     if (fSeqList) { fSeqList->FreeAllObjects(); fSeqList->suicide(); }
  1580. }
  1581.  
  1582. void DSeqDoc::FreeData()
  1583. {
  1584.     if (fSeqList) { fSeqList->FreeAllObjects(); }
  1585. }
  1586.  
  1587.  
  1588. Boolean DSeqDoc::IsSeqFile(DFile* aFile)
  1589. {
  1590.     // this is a static function, usable w/o a DGopherListDoc object
  1591.     if (fgTestSeqFile && aFile && aFile->Exists()) {
  1592.         aFile->OpenFile();
  1593.         short format= DSeqFile::SeqFileFormatWrapper( aFile);
  1594.         return (format != DSeqFile::kUnknown);
  1595.         }
  1596.     else
  1597.         return false;
  1598. }
  1599.  
  1600. void DSeqDoc::MakeGlobalsCurrent()
  1601. {
  1602.     ViewRect( fViewrect); // get current rect...
  1603.     if (!Nlm_EmptyRect(&fViewrect)) fgWinRect= fViewrect;  
  1604.     if (fLockCheck) fgLockText= fLockCheck->GetStatus();
  1605.     if (fColorCheck) fgUseColor= fColorCheck->GetStatus();
  1606.     if (fModePop) fgViewMode= fModePop->GetValue();
  1607. }
  1608.  
  1609. void DSeqDoc::Close()
  1610. {
  1611. #if 1
  1612.     DList* winlist= gWindowManager->GetWindowList();
  1613.     short  i, nwin= winlist->GetSize();
  1614.     for (i=0; i<nwin; i++) {
  1615.         DWindow* awin= (DWindow*) winlist->At(i);
  1616.         if (awin->Id() == DSeqedWindow::kId 
  1617.             && ((DSeqedWindow*) awin)->fMainDoc == this) { 
  1618.                 delete awin; // ?? or awin->Close();
  1619.                  }
  1620.         }
  1621. #endif
  1622.     
  1623.     MakeGlobalsCurrent();
  1624.     DWindow::Close();
  1625. }
  1626.  
  1627. void DSeqDoc::ResizeWin()
  1628. {
  1629.     DWindow::ResizeWin();
  1630.     MakeGlobalsCurrent();
  1631. }
  1632.  
  1633. void DSeqDoc::Open(DFile* aFile)
  1634. {
  1635.     Boolean isempty= (fSeqList->GetSize() < 1);
  1636.     if ( ReadFrom( aFile, true) ) {
  1637.         if (isempty) this->SetTitle((char*)aFile->GetShortname());
  1638.         this->Open();
  1639.         }
  1640.     else 
  1641.         if (isempty) this->suicide();
  1642. }
  1643.  
  1644.  
  1645. // static
  1646. void DSeqDoc::NewSeqDoc()
  1647. {
  1648.     DSeqDoc* newdoc= new DSeqDoc( DSeqDoc::kSeqdoc, NULL, (char*)DFileManager::kUntitled);
  1649.     newdoc->Open();
  1650.     newdoc->EditSeqs(); //?? make it real obvious to beginners?
  1651. }
  1652.  
  1653. // static
  1654. void DSeqDoc::GetGlobals()
  1655. {
  1656.     char* onoffs;
  1657.  
  1658.     SeqDocPrefs(kSeqDocPrefInit);
  1659.     DSeqPrintDoc::GetGlobals();
  1660.     SeqPrintPrefs(kSeqPrintPrefInit);
  1661.  
  1662.     onoffs= (fgLockText) ? "1" : "0";
  1663.     fgLockText= gApplication->GetPrefVal( "fgLockText", "windows", onoffs);
  1664.     onoffs= (fgUseColor) ? "1" : "0";
  1665.     fgUseColor= gApplication->GetPrefVal( "fgUseColor", "windows", onoffs);
  1666.     fgViewMode= gApplication->GetPrefVal( "fgViewMode", "seqdoc", "0");
  1667.  
  1668.     {
  1669.     //350, 200
  1670.     char* srect = gApplication->GetPref( "fgWinRect", "windows", "30 40 450 220");
  1671. #if 1
  1672.         // sscanf is failing on Mac/codewar !! used to work
  1673.     if (srect) {
  1674.         char* cp= srect;
  1675.         while (*cp && isspace(*cp)) cp++;
  1676.         fgWinRect.left= atoi( cp);
  1677.         
  1678.         while (*cp && !isspace(*cp)) cp++;
  1679.         while (*cp && isspace(*cp)) cp++;
  1680.         fgWinRect.top= atoi( cp);
  1681.         
  1682.         while (*cp && !isspace(*cp)) cp++;
  1683.         while (*cp && isspace(*cp)) cp++;
  1684.         fgWinRect.right= atoi( cp);
  1685.         
  1686.         while (*cp && !isspace(*cp)) cp++;
  1687.         while (*cp && isspace(*cp)) cp++;
  1688.         fgWinRect.bottom= atoi( cp);
  1689.         }
  1690. #else
  1691.         if (srect) sscanf( srect, "%d%d%d%d", &fgWinRect.left, &fgWinRect.top, 
  1692.                                                 &fgWinRect.right, &fgWinRect.bottom);
  1693. #endif
  1694.         MemFree(srect);
  1695.     }
  1696. }
  1697.  
  1698. // static
  1699. void DSeqDoc::SaveGlobals()
  1700. {
  1701.     gApplication->SetPref( (int) fgLockText,"fgLockText","windows");
  1702.     gApplication->SetPref( (int) fgUseColor, "fgUseColor","windows");
  1703.     gApplication->SetPref( (int) fgViewMode, "fgViewMode","seqdoc");
  1704.  
  1705.     if (!Nlm_EmptyRect(&fgWinRect)) {
  1706.         char  srect[128];
  1707.         sprintf( srect, "%d %d %d %d", fgWinRect.left, fgWinRect.top, 
  1708.                                                         fgWinRect.right, fgWinRect.bottom);
  1709.         gApplication->SetPref( srect, "fgWinRect", "windows");
  1710.         }
  1711. }
  1712.  
  1713.  
  1714. void DSeqDoc::Open()
  1715. {
  1716.     DView* super;
  1717.     short width, height;
  1718.     Nlm_PoinT        nps;
  1719.     DPrompt* pr;
  1720.     Boolean selectone= false;
  1721.     
  1722.     if (!fSeqList) fSeqList= new DSeqList();
  1723.     if (fSeqList->GetSize() == 0) {
  1724.         fSeqList->AddNewSeq();    // install 1 blank seq for new doc
  1725.         selectone= true;
  1726.         }
  1727.     if (!fAlnView || !fAlnIndex || !fAlnHIndex || !fAlnITitle) {
  1728.         super= this;
  1729.         fColorCheck= new DCheckBox(kColorButHit,super,"Color bases");
  1730.         fColorCheck->SetStatus(fgUseColor);
  1731.         fUseColor= fgUseColor;
  1732.         super->NextSubviewToRight();
  1733. #if MASKS
  1734.         pr= new DPrompt(0,super," Mode:",0, 0, Nlm_programFont);
  1735.         super->NextSubviewToRight();
  1736.         fModePop= new DAlnModePopup(kModePopup,super,fgViewMode);
  1737.         fLockCheck= NULL;
  1738. #else
  1739.         fLockCheck= new DCheckBox(kLockButHit,super,"Lock text");
  1740.         fLockCheck->SetStatus(fgLockText);
  1741. #endif
  1742.         super->NextSubviewToRight();
  1743.         pr= new DPrompt(0,super,"  File format:",0, 0, Nlm_programFont);
  1744.         super->NextSubviewToRight();
  1745.         fFormatPop= new DSeqFormatPopup(0,super,fInFormat);
  1746.         super->NextSubviewBelowLeft();
  1747.         
  1748.             // left group : titleline over name table
  1749.         DCluster* grouper= new DCluster( 0, this, 0, 0, true);   // was 0,0 for w,h
  1750.         grouper->SetResize( DView::fixed, DView::relsuper);
  1751.         super= grouper;
  1752.  
  1753.         fAlnITitle= new DAlnITitle(0, super, 80, Nlm_stdLineHeight);
  1754.         super->NextSubviewBelowLeft();
  1755.  
  1756.         //width= 350; height= 200;
  1757.         super->GetNextPosition( &nps);
  1758.         width = MAX( 40, fgWinRect.right - fgWinRect.left) - Nlm_vScrollBarWidth  - nps.x; 
  1759.         height= MAX( 60, fgWinRect.bottom - fgWinRect.top) - Nlm_hScrollBarHeight - nps.y;   
  1760.         fAlnIndex= new DAlnIndex(0, super, this, fSeqList, 80, height); // pixwidth,height
  1761.                                                                                                       //  ^^ needs to be user changable ??
  1762.         super= this;
  1763.         super->NextSubviewToRight();        
  1764.         
  1765.             // right group : index topline over bases table
  1766.         grouper= new DCluster( 0, this, 0, 0, true); // was 0,0 wid,hgt
  1767.         grouper->SetResize( DView::relsuper, DView::relsuper);
  1768.         super= grouper;
  1769.         super->GetNextPosition( &nps);
  1770.         width = MAX( 40, fgWinRect.right - fgWinRect.left) - Nlm_vScrollBarWidth  - nps.x; 
  1771.         height= MAX( 60, fgWinRect.bottom - fgWinRect.top) - Nlm_hScrollBarHeight - nps.y; 
  1772.         fAlnHIndex= new DAlnHIndex(0, super, this, fSeqList, width, Nlm_stdLineHeight);
  1773.         super->NextSubviewBelowLeft();
  1774.         
  1775.         //width= 350; height= 200;
  1776.         super->GetNextPosition( &nps);
  1777.         width = MAX( 40, fgWinRect.right - fgWinRect.left) - Nlm_vScrollBarWidth  - nps.x; 
  1778.         height= MAX( 60, fgWinRect.bottom - fgWinRect.top) - Nlm_hScrollBarHeight - nps.y; 
  1779.         fAlnView= new DAlnView(0, super, this, fSeqList, width, height); // pixwidth,height
  1780.         fAlnView->SetTextLock( fgLockText);
  1781.         super= this;
  1782.  
  1783. #if ETEXT
  1784.         //if (gLastEditView) gLastEditView->DeInstallEditSeq();
  1785.         {
  1786.         Nlm_PoinT  nps, saveps;
  1787.         GetNextPosition( &saveps);
  1788.         nps.x= 1; nps.y= 1; // locate dialog item 
  1789.         SetNextPosition( nps);
  1790.         //gDialogTextMultiline= false;
  1791.         DAlnSequence* eseq= new DAlnSequence( 0, this);  
  1792.         fAlnView->fEditSeq= eseq;
  1793.         //this->SetEditText(eseq);
  1794.         eseq->fVisible= true;
  1795.         eseq->HideEdit(); // eseq->Disable(); eseq->Hide();
  1796.         this->SetEditText(NULL);  
  1797.         SetNextPosition( saveps);
  1798.         }
  1799. #endif
  1800.  
  1801.         }
  1802.     fSaveHandler= this;  
  1803.     fPrintHandler= this; 
  1804.     
  1805.     if (!fFindDlog) fFindDlog= new DAlnFindDialog( NULL, fAlnView);
  1806.  
  1807.     this->Select(); // for motif
  1808.     this->CalcWindowSize();
  1809.     fAlnView->GetReadyToShow(); // do before others ...
  1810.     if (fAlnIndex) fAlnIndex->GetReadyToShow();
  1811.     fAlnITitle->GetReadyToShow();
  1812.     //fAlnHIndex->GetReadyToShow();
  1813.     fAlnView->SetViewMode(fgViewMode);
  1814.     if (selectone && fAlnIndex) fAlnIndex->SelectCells(0,0);
  1815.     
  1816.     DWindow::Open();
  1817. }
  1818.  
  1819.  
  1820. void DSeqDoc::Activate()
  1821. {
  1822.     if (fAlnView && fAlnView->fEditSeq && fAlnView->fEditSeq->fVisible) 
  1823.         fAlnView->fEditSeq->selectAction();
  1824.     gCursor->arrow();
  1825.     DWindow::Activate();
  1826. }
  1827.  
  1828. void DSeqDoc::Deactivate()
  1829. {
  1830.     if (fAlnView && fAlnView->fEditSeq) fAlnView->fEditSeq->deselectAction();
  1831.     DWindow::Deactivate();
  1832. }
  1833.  
  1834. void DSeqDoc::Print()
  1835. {
  1836.     // fix this to print more of window !
  1837.     //fAlnView->Print();
  1838.     
  1839.     // or do prettyprint...
  1840.     MakeSeqPrint( false);
  1841.     fPrintDoc->PrintDoc();
  1842. }
  1843.  
  1844. Boolean DSeqDoc::IsMyTask(DTask* theTask) 
  1845. {
  1846. #if 0
  1847.     if (theTask->fKind == kSeqDoc) {
  1848.         ProcessTask( theTask);
  1849.         return true;
  1850.         }
  1851.     else 
  1852. #endif
  1853.         return DWindow::IsMyTask(theTask);
  1854. }
  1855.  
  1856. void DSeqDoc::ProcessTask(DTask* theTask) 
  1857. {
  1858.     if (theTask->fNumber == -987654) {
  1859.  
  1860.         }
  1861.     else {
  1862.         DWindow::ProcessTask(theTask);
  1863.         }
  1864. }
  1865.  
  1866.  
  1867. void DSeqDoc::SortView(DSeqList::Sorts sortorder)
  1868. {
  1869.     fSeqList->SortList(sortorder);
  1870.     fAlnView->Invalidate();
  1871.     if (fAlnIndex) fAlnIndex->Invalidate();
  1872. }
  1873.  
  1874.  
  1875. void DSeqDoc::AddSeqToList(DSequence* item)
  1876. {
  1877.     fAlnView->addToAlnList( item);
  1878.     //fAlnView->SelectCells( fAlnView->GetMaxRows()-1, fAlnView->GetMaxCols());
  1879.     if (fAlnIndex) fAlnIndex->SelectCells( fAlnView->GetMaxRows()-1, 0); 
  1880. #if 0
  1881.     this->Changed( 1, this);
  1882. #endif
  1883. }    
  1884.  
  1885.  
  1886.  
  1887. void DSeqDoc::AddNewSeqToList()
  1888. {
  1889.     DSequence* aSeq = new DSequence(); 
  1890.     AddSeqToList( aSeq);
  1891. }
  1892.  
  1893.  
  1894. void DSeqDoc::FirstSelection( DSequence*& aSeq, long& start, long& nbases)
  1895. {    
  1896.     short            left, right, top, bottom, atRow;
  1897.  
  1898.     start= nbases= atRow= 0;
  1899.     fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  1900.     fAlnView->GetFirstSelectedCell(top,left);
  1901.     fAlnView->GetLastSelectedCell(bottom,right);
  1902.     
  1903.     if (left != DTableView::kNoSelection && top != DTableView::kNoSelection 
  1904.      && (left != right || top != bottom)) {
  1905.       atRow = top;
  1906.         start = left; // - 1;
  1907.         nbases= right - start;  
  1908.         }    
  1909.     else if (fAlnIndex && fAlnIndex->IsSelected()) {
  1910.         fAlnIndex->GetFirstSelectedCell(top,left);
  1911.         fAlnIndex->GetLastSelectedCell(bottom,right);
  1912.         atRow= top;
  1913.         start= 0; 
  1914.         nbases= 0; //key for all of seq
  1915.         }    
  1916.  
  1917.     aSeq= fAlnView->SeqAt( atRow);
  1918.     if (aSeq) {
  1919.         aSeq->SetIndex(atRow);
  1920.         aSeq->SetSelection( start, nbases);
  1921.         }
  1922. }
  1923.  
  1924. void DSeqDoc::AddSeqAtToList( short aRow, long start1, long nbases1, 
  1925.                                                         DSeqList*& aSeqList, long& start, long& nbases)
  1926. {
  1927.     DSequence* aSeq= fAlnView->SeqAt( aRow); 
  1928.     if (aSeq) {
  1929.         /*--- ?? default is use all when nbases==0 !?
  1930.         if (wantAll) {
  1931.             start1= 0;
  1932.             nbases1= GethandleSize(aSeq->fBases);
  1933.             }
  1934.         ---*/
  1935.         aSeq->SetIndex( aRow);
  1936.         aSeq->SetSelection( start1, nbases1);
  1937.         aSeqList->InsertLast( aSeq);
  1938.         start= Max(start,start1);
  1939.         if (nbases==0) nbases= nbases1;
  1940.         else nbases= Min(nbases,nbases1);
  1941.         }
  1942. }
  1943.  
  1944. void DSeqDoc::GetSelection(Boolean equalCount, Boolean allAtNoSelection,
  1945.                                                         DSeqList*& aSeqList, long& start, long& nbases)
  1946. {
  1947.     short            left, right, top, bottom, arow, acol;
  1948.     long            start1, nbases1;
  1949.     Boolean        wantAll;
  1950.  
  1951.     fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  1952.     start= 0; start1= 0;
  1953.     nbases= 0; nbases1= 0;
  1954.     aSeqList= new DSeqList();
  1955.     fAlnView->GetFirstSelectedCell(top,left);
  1956.     fAlnView->GetLastSelectedCell(bottom,right);
  1957.     
  1958.     if (left != DTableView::kNoSelection && top != DTableView::kNoSelection 
  1959.     && (left != right || top != bottom)) {
  1960.         wantAll= false;
  1961.         for (arow= top; arow<bottom; arow++) {
  1962.             if (equalCount) acol= left; else acol= 0; 
  1963.             while (acol<right && !fAlnView->IsSelected(arow,acol)) acol++;
  1964.             start1= acol; // - 1;
  1965.             if (equalCount)
  1966.                 while (acol<right && fAlnView->IsSelected(arow,acol)) acol++;
  1967.             else 
  1968.                 while (fAlnView->IsSelected(arow,acol)) acol++;
  1969.             nbases1= acol - start1;
  1970.             AddSeqAtToList( arow, start1, nbases1, aSeqList, start, nbases);
  1971.             }
  1972.         start = left; // - 1;
  1973.         nbases= right - start;  
  1974.         }    
  1975.         
  1976.     else if (fAlnIndex && fAlnIndex->IsSelected()) {
  1977.         fAlnIndex->GetFirstSelectedCell(top,left);
  1978.         fAlnIndex->GetLastSelectedCell(bottom,right);
  1979.         wantAll= true;
  1980.         for (arow= top; arow<bottom; arow++) 
  1981.             AddSeqAtToList( arow, start1, nbases1, aSeqList, start, nbases);
  1982.         }    
  1983.     else {
  1984.         wantAll= true;
  1985.         if (allAtNoSelection) 
  1986.          for (arow= 0; arow<fAlnView->GetMaxRows(); arow++) 
  1987.             AddSeqAtToList( arow, start1, nbases1, aSeqList, start, nbases);
  1988.         }
  1989. }
  1990.             
  1991.  
  1992. short DSeqDoc::SelectionToFile(Boolean AllatNoSelection, char* aFileName, short seqFormat)  
  1993. {
  1994.     long        start, nbases;
  1995.     short        nseqs;
  1996.     DSeqList *aSeqList;
  1997.  
  1998.     GetSelection( FALSE, AllatNoSelection, aSeqList, start, nbases);
  1999.     nseqs= aSeqList->GetSize();
  2000.     aSeqList->DoWrite( aFileName, seqFormat); 
  2001.     delete aSeqList;
  2002.     return nseqs;
  2003. }
  2004.  
  2005.  
  2006.  
  2007.  
  2008. void DSeqDoc::WriteTo(DFile* aFile)  
  2009. {
  2010.     short outformat= DSeqFile::kGenBank;
  2011.     if (fFormatPop) outformat= fFormatPop->GetValue(); 
  2012.     fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  2013.     if (fSaveSelection) {
  2014.         long        start, nbases;
  2015.         DSeqList* aSeqList= NULL;
  2016.         fSaveSelection= false;
  2017.         GetSelection( FALSE, true, aSeqList, start, nbases);
  2018.         aSeqList->DoWrite( aFile, outformat);
  2019.         delete aSeqList;
  2020.         }    
  2021.     else {
  2022.         fSeqList->ClearSelections(); //! make sure we write all of seq from this call !?
  2023.         fSeqList->DoWrite( aFile, outformat);
  2024.         this->NotDirty();  
  2025.         }
  2026. }
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032. #ifdef FIX_LATER
  2033. /***********
  2034. void DSeqDoc::AboutToSaveFile(TFile theSaveFile, CommandNumber itsCmd, 
  2035.                                                             Boolean VAR makingCopy) // override 
  2036. VAR
  2037.         Boolean        needSameSize, sizesDiffer, isInterleaved;
  2038.         integer        nseqs, err, format ;
  2039.         longint        minbases;
  2040.         Handle        aHand;
  2041.         
  2042.     void checkEqualSize( DSequence aSeq)
  2043.     longint        var  start, nbases;
  2044.     {
  2045.         //aSeq->GetSelection( start, nbases); << can still be set from fSelection or other old selecting command 
  2046.         nBases= aSeq->fLength; 
  2047.         if (minbases<0) then minbases= nbases
  2048.         else begin
  2049.             if (nbases!=minbases) then sizesDiffer= true;
  2050.             //- minbases= min( nbases, minbases); 
  2051.             end();
  2052.     }
  2053.  
  2054. {
  2055.     inherited::AboutToSaveFile(theSaveFile, itsCmd, makingCopy);
  2056.     
  2057.     if ((!fSaveSelection)) { 
  2058.                     //^^^^ !???? TAlnDoc will set this, always equal #bases????
  2059.         if (fFormatPop == NULL) format= kGenBank
  2060.         else format= fFormatPop->GetCurrentItem(); 
  2061.         needSameSize= false;
  2062.         isInterleaved= false;
  2063.         sizesDiffer= false;
  2064.         minbases= -1; 
  2065.         nseqs= fSeqList->GetSize();
  2066.         
  2067.         if (nseqs>1)) // deal w/ one-per-file formats 
  2068.             switch (format) {
  2069.                 kGCG        : format= kMSF;
  2070.                 kStrider: format= kIG;
  2071.                 kNoformat, kPlain, kUnknown: format= kGenbank;
  2072.                 }
  2073.             
  2074.         fSeqList->ClearSelections(); //! make sure we write all of seq from this call !?
  2075.         fSeqList->Each( checkEqualSize);    
  2076.     
  2077.         aHand= NewHandle(0);
  2078.         WriteSeqHeader( longint(aHand), TRUE, format, nseqs, minbases, gOutputName, 
  2079.                     needSameSize, isInterleaved); //<< need write for last two results 
  2080.         aHand= DisposeIfHandle(aHand);
  2081.         
  2082.         if (((needSameSize || isInterleaved) && sizesDiffer)) {
  2083.             ParamText('all sequences must have same # bases for this format',
  2084.                     ' Pick another format like Genbank or change selection.',
  2085.                     'WriteSeq','');    
  2086.             Failure( -1, msgMyError);  
  2087.             }
  2088.         }
  2089. }
  2090. **********/
  2091. #endif
  2092.  
  2093.  
  2094. Boolean DSeqDoc::ReadFrom(DFile* aFile, Boolean append)    // revise for iostreams
  2095. {
  2096.     if (!append) FreeData();   //?? or leave here for append...
  2097.     gDefSeqName= this->GetTitle(gDefSeqNameStore,sizeof(gDefSeqNameStore));
  2098.     fInFormat= DSeqFile::ReadSeqFile(aFile, fSeqList);  
  2099.     return true; //?? (fInFormat != DSeqFile::kUnknown)
  2100. }
  2101.  
  2102.  
  2103.  
  2104. #if 0
  2105. void DSeqDoc::Close() // override 
  2106. //must make sure all windows owned by ListDoc are closed 1st
  2107.  
  2108.     procedure closeSubwindows(TWindow aWind)
  2109.     begin
  2110.       // this is a subwindow !may not be owned by this  doc
  2111.         if (!aWind->fClosesDocument) aWind->CloseAndFree();
  2112.     end();
  2113.     
  2114. {
  2115.     ForAllWindowsDo(closeSubwindows);
  2116.     inherited::Close();
  2117. }
  2118.  
  2119. #endif
  2120.  
  2121.  
  2122.  
  2123. void DSeqDoc::OpenSeqedWindow(DSequence* aSeq)
  2124. {
  2125.     char* title= StrDup( aSeq->Name());
  2126.     StrExtendCat( &title, " Edit");
  2127.     DSeqedWindow* edwin = new DSeqedWindow( DSeqedWindow::kId, gApplication, 
  2128.                                                 this, aSeq, -5, -5, -50, -20, title);
  2129.     MemFree( title);
  2130.     edwin->Open(); 
  2131.     edwin->Select();     
  2132. }
  2133.  
  2134.  
  2135.  
  2136. void DSeqDoc::EditSeqs() 
  2137. {
  2138.     DSequence* aSeq= fAlnView->SelectedSequence();  
  2139.     if (aSeq) this->OpenSeqedWindow(aSeq);
  2140.                 
  2141. #if 0
  2142.     aCell    : GridCell;
  2143.     count    : integer;
  2144.     
  2145.     pascal void newEditWindow( integer anItem)
  2146.     TSequence        VAR aSeq;
  2147.     {
  2148.         aSeq= TSequence(this->fSeqList->At(anItem));
  2149.         if ((aSeq!=NULL) && (count<kMaxOpenSeqed)) {
  2150.             this->OpenSeqedWindow(aSeq);
  2151.             count= count+1;
  2152.             }
  2153.   }
  2154.     
  2155.  
  2156.     if ((fAlnIndex=NULL) || (fAlnIndex.firstSelectedItem == 0)) 
  2157.             //nada
  2158.             
  2159.     else if (editOnly 
  2160.         || (fAlnIndex.FirstSelectedItem == fAlnIndex->LastSelectedItem())) {
  2161.         count= 0;
  2162.         fAlnIndex->EachSelectedItemDo( newEditWindow);
  2163.         }
  2164. #endif        
  2165. }
  2166.  
  2167.  
  2168.  
  2169.  
  2170. void DSeqDoc::ToTextDoc()
  2171. {
  2172. #if 1
  2173.     DTempFile* tmpFile= new DTempFile();
  2174.     this->WriteTo(tmpFile);
  2175.     DRichTextDoc* doc= new DRichTextDoc(0,  true, gTextFont);
  2176.     doc->Open(tmpFile);
  2177.     delete tmpFile;
  2178. #else
  2179.     Nlm_ParData paratabs = {false, false, false, false, true, 0, 0}; // tabs==tabs        
  2180.     char    *txt     = NULL;
  2181.     Nlm_ColData*    cols= NULL;
  2182.     Nlm_ParData*    para= ¶tabs;
  2183.  
  2184.     ulong txtsize = 0;
  2185.     DTempFile* tmpFile= new DTempFile();
  2186.     this->WriteTo(tmpFile);
  2187.     txt= tmpFile->ReadIntoMemory(txtsize);
  2188.     delete tmpFile;
  2189.     
  2190.     if (txt) {
  2191.         char name[256];
  2192.         StrCpy(name,"Text of ");
  2193.         long len= StrLen(name);
  2194.         //char *namep= name + len;
  2195.         (void) this->GetTitle( name + len, 256 - len);
  2196.         DWindow* aWin= new DWindow( 0, gApplication, document, -1, -1, -10, -10, name);
  2197.         DTextDocPanel* aDoc= new DTextDocPanel( 0, aWin, 500, 300);
  2198.         aDoc->SetResize( DView::matchsuper, DView::relsuper);
  2199.         aDoc->SetSlateBorder( false);
  2200.         aDoc->SetTabs( gTextTabStops);
  2201.         aDoc->Append( txt, para, cols, gTextFont);
  2202.         aWin->Open();  
  2203.         aDoc->SizeToSuperview( aWin, true, false);
  2204.         }
  2205. #endif
  2206. }
  2207.  
  2208.  
  2209. void DSeqDoc::MakeSeqPrint(Boolean doREMap)
  2210. {
  2211.     long        firstbase, nbases;
  2212.     DSeqList* aSeqList= NULL;
  2213.     fSaveSelection= false;
  2214.     DWindow    * aDoc;
  2215.     
  2216.     GetSelection( true, true, aSeqList, firstbase, nbases);
  2217.     if (!aSeqList)
  2218.         return;
  2219.     else if (doREMap) {
  2220.         aDoc = new DREMapPrintDoc( DSeqPrintDoc::kSeqPrintDoc, this, aSeqList, firstbase, nbases);
  2221.         aDoc->Open();  
  2222.         }
  2223.     else if (aSeqList->GetSize() > 1) {
  2224.         aDoc = new DAlnPrintDoc( DSeqPrintDoc::kSeqPrintDoc, this, aSeqList, firstbase, nbases);
  2225.         aDoc->Open();  
  2226.         }        
  2227.     else {
  2228.         aDoc = new DSeqPrintDoc( DSeqPrintDoc::kSeqPrintDoc, this, aSeqList, firstbase, nbases);
  2229.         aDoc->Open();  
  2230.         }
  2231.     fPrintDoc= aDoc;
  2232. }
  2233.  
  2234. void DSeqDoc::MakeAlnPrint()
  2235. {
  2236.     MakeSeqPrint(false);
  2237. }
  2238.  
  2239.  
  2240.  
  2241. void DSeqDoc::SetUpMenu(short menuId, DMenu*& aMenu) 
  2242. {
  2243.     //DWindow::SetUpMenu(menuId, aMenu);
  2244.  
  2245.     if (menuId == kSeqMenu) {
  2246.         if (!aMenu) aMenu = gApplication->NewMenu( menuId, "Sequence");
  2247.         //aMenu->AddItem( cSeqPrefs, "Seq Prefs...");    
  2248.     
  2249.         aMenu->AddItem( cNewSeq, "New sequence");
  2250.         aMenu->AddItem( cEditSeq, "Edit seq...");
  2251.      
  2252.         DMenu* vMenu= new DSubMenu( kViewKindMenu, aMenu, "View seqs");
  2253.         vMenu->AddItem( kViewByDefault, "default");
  2254.         vMenu->AddItem( kViewByDate, "by Date");
  2255.         vMenu->AddItem( kViewBySize, "by Size");
  2256.         vMenu->AddItem( kViewByName, "by Name");
  2257.         vMenu->AddItem( kViewByKind, "by Kind");
  2258.         vMenu->AddItem( kViewAsText, "as Text");
  2259.  
  2260.         DMenu* mMenu= new DSubMenu( kSeqMaskMenu, aMenu, "Seq masks");
  2261.         mMenu->AddItem(cMaskSelAll,"Select all",false, true);
  2262.         mMenu->AddItem(cMaskInvert,"Invert selection",false, true);
  2263.         mMenu->AddItem(cMaskClear,"Clear selection",false, true);
  2264.         mMenu->AddSeparator();
  2265.         mMenu->AddItem(cMaskSelCommon,"Select consensus",false, true);
  2266.         //gViewCentral->DisableView(cMaskSelCommon);
  2267.         mMenu->AddItem(cMaskSelORF,"Select ORFs",false, true);
  2268.         //gViewCentral->DisableView(cMaskSelORF);
  2269.  
  2270.         aMenu->AddSeparator();
  2271.         aMenu->AddItem( cRevSeq, "Reverse");
  2272.         aMenu->AddItem( cCompSeq, "Complement");
  2273.         aMenu->AddItem( cRevCompSeq, "Rev-Compl");
  2274.         aMenu->AddItem( cDna2Rna, "Dna->Rna");
  2275.         aMenu->AddItem( cRna2Dna, "Rna->Dna");
  2276.         aMenu->AddItem( cToUpper, "UPPER Case");
  2277.         aMenu->AddItem( cToLower, "lower Case");
  2278.  
  2279.         aMenu->AddItem( cDegap, "Degap");
  2280.         aMenu->AddItem( cLockIndels, "Lock indels");
  2281.         aMenu->AddItem( cUnlockIndels, "Unlock indels");
  2282.         aMenu->AddItem( cConsensus, "Consensus");
  2283.         aMenu->AddItem( cTranslate, "Translate");
  2284.  
  2285.         aMenu->AddSeparator();
  2286.         aMenu->AddItem( cPrettyPrint, "Pretty Print...");
  2287.         aMenu->AddItem( cREMap, "Restriction map...");
  2288.         aMenu->AddItem( cDotPlot, "Dot plot...");
  2289.         gViewCentral->DisableView(cDotPlot);
  2290.  
  2291.         aMenu->AddSeparator();        
  2292.         aMenu->AddItem( cNAcodes, "Nucleic codes...");
  2293.         aMenu->AddItem( cAAcodes, "Amino codes...");
  2294.          }
  2295.      
  2296.     else if (menuId == DApplication::cEditMenu) {
  2297.         aMenu->AddSeparator();
  2298.         aMenu->AddItem(DApplication::kFind,"Find.../F",false, true);
  2299.         aMenu->AddItem(DApplication::kFindAgain,"Find again/G",false, true);
  2300.         aMenu->AddItem(cFindORF,"Find ORF/R",false, true);
  2301.  
  2302.         }
  2303.         
  2304.      else if (menuId == kInternetMenu) {
  2305.         if (!aMenu) aMenu = gApplication->NewMenu( menuId, "Internet");
  2306.         aMenu->AddItem( cNCBIfetch, "NCBI fetch...");
  2307.         aMenu->AddItem( cNCBIblast, "NCBI BLAST search...");
  2308.         aMenu->AddItem( cEMBLfetch, "EMBL fetch...");
  2309.         aMenu->AddItem( cEMBLquicks, "EMBL Quicksearch...");
  2310.         aMenu->AddItem( cEMBLfasta, "EMBL FastA search...");
  2311.         aMenu->AddItem( cEMBLblitz, "EMBL Blitz search...");
  2312.         aMenu->AddItem( cFHCRCfetch, "FHCRC fetch...");
  2313.         aMenu->AddItem( cFHCRCblocks, "FHCRC Blocks search...");
  2314.         aMenu->AddItem( cGeneidSearch, "Geneid search...");
  2315.         aMenu->AddItem( cGrailSearch, "Grail search...");
  2316.         //aMenu->AddItem( cGenmarkSearch, "Genmark search...");
  2317.         //aMenu->AddItem( cPythiaSearch, "Pythia search...");
  2318.          aMenu->AddSeparator();    
  2319.          }                            
  2320.  
  2321. }
  2322.  
  2323.  
  2324.  
  2325. #if 0
  2326. char* DSeqDoc::GetTitle(char* title, ulong maxsize) 
  2327.     if (title==NULL) title= (char*) MemNew(maxsize);  
  2328. kUntitled
  2329.     Nlm_GetTitle( fNlmObject, title, maxsize);
  2330.     return title;
  2331. }
  2332. #endif
  2333.  
  2334.  
  2335. char* DSeqDoc::GetTitle(char* title, ulong maxsize) 
  2336.     if (fSaveSelection) {
  2337.         if (title==NULL) title= (char*) MemNew(maxsize); 
  2338.         DWindow::GetTitle( title, maxsize); 
  2339. #ifdef OS_DOS
  2340.         DFileManager::ReplaceSuffix( title, maxsize, ".sel");
  2341. #else
  2342.         StrNCat( title,".part", maxsize); 
  2343. #endif
  2344.         return title;
  2345.         }
  2346.     else if (fDocTitle && *fDocTitle) {
  2347.         if (title==NULL) {
  2348.             maxsize= Min(maxsize,StrLen(fDocTitle)+1);
  2349.             title= (char*) MemNew(maxsize); 
  2350.             }
  2351.         StrNCpy(title,fDocTitle,maxsize);
  2352.         return title;
  2353.         }
  2354.     else {
  2355.         DWindow::GetTitle( title, maxsize);
  2356.         //if (StringCmp(title,kUntitled)==0) *title= 0; // set to null so saveas is called
  2357.         return title;
  2358.         }
  2359. }
  2360.  
  2361. void DSeqDoc::FindORF()
  2362. {
  2363.     long    start, stop;
  2364.     DSequence* aSeq;
  2365.     short            selrow;
  2366.     Nlm_RecT    selr;
  2367.     
  2368.     aSeq= fAlnView->SelectedSequence(selrow);
  2369.     if (aSeq) {
  2370.         selr= fAlnView->GetSelRect();
  2371.         if (selr.right > selr.left+1) selr.left++; // !? offset sel start so we can repeat call
  2372.         selr.top= selrow;
  2373.         selr.bottom= selrow+1; 
  2374.         aSeq->SetSelection( selr.left, 0);
  2375.         aSeq->SearchORF( start, stop);
  2376.         if (start>=0) {
  2377.             if (stop<start) stop= aSeq->LengthF(); //??
  2378.             aSeq->SetSelection( start, stop - start);
  2379.             selr.left= start;
  2380.             selr.right= stop;
  2381.             fAlnView->SelectCells( selr);
  2382.             }
  2383.         } 
  2384. }
  2385.  
  2386.  
  2387. void DSeqDoc::MakeConsensus()
  2388. {
  2389.     long    firstBase = 0, nBases = 0;
  2390.     DSeqList * aSeqList= NULL;
  2391.     DSequence* cons;
  2392.     
  2393.     GetSelection( true, true, aSeqList, firstBase, nBases);
  2394.     if (aSeqList) {
  2395.         cons= fSeqList->Consensus();
  2396.         if (cons) {
  2397.             short aRow, totalRows;
  2398.             Nlm_RecT    r;
  2399.             aRow= fSeqList->GetIdentityItemNo( cons);
  2400.             totalRows= fSeqList->GetSize();
  2401.             fSeqList->Delete(cons);
  2402.             delete cons;
  2403.             fAlnView->GetRowRect( aRow, r, totalRows-aRow+1);
  2404.             fAlnView->InvalRect( r);
  2405.             if (fAlnIndex) {
  2406.                 fAlnIndex->GetRowRect( aRow, r, totalRows-aRow+1);
  2407.                 fAlnIndex->InvalRect( r);
  2408.                 }
  2409.             fAlnView->UpdateSize();   
  2410.             }
  2411.             
  2412.         aSeqList->MakeConsensus();
  2413.         cons= aSeqList->Consensus();
  2414.         if (cons) {
  2415.             AddSeqToList( cons);
  2416.             //if (fAlnIndex) fAlnIndex->SelectCells( fSeqList->ConsensusRow(), 0); 
  2417.             //fAlnIndex->ScrollSelectionIntoView(TRUE);
  2418.             }
  2419.         delete aSeqList;
  2420.         }
  2421. }
  2422.  
  2423.  
  2424. #if NETBLAST
  2425. extern "C" short CallNCBIBlastService( char* blastProgram, char* blastDatabase, char* blastOptions, 
  2426.                                                      char* fastaInputFile, char* outputFile );
  2427.  
  2428. void DSeqDoc::BLASTdialog()
  2429. {
  2430.     char* blastProgram = "blastn";
  2431.     char* blastDatabase = "nr";
  2432.     char* blastOptions = NULL;
  2433.     char* fastaInputFile = "spup-blast-input.fasta";
  2434.     char* outputFile = "spup-blast-result.text";
  2435.     char    instore[128];
  2436.     char  outstore[128];
  2437.     DFile aFile;
  2438.             
  2439.     fastaInputFile= DFileManager::TempFilename( instore);
  2440.     outputFile= DFileManager::TempFilename( outstore);
  2441.  
  2442.     short nseq= SelectionToFile( false, fastaInputFile, DSeqFile::kPearson);
  2443.     if (nseq) {
  2444.         short err= CallNCBIBlastService(blastProgram, blastDatabase, blastOptions, 
  2445.                                                 fastaInputFile, outputFile);
  2446.  
  2447.         if (err) {
  2448.             Message(MSG_OK,"DSeqDoc::CallNCBIBlastService error %d", err);
  2449.             }
  2450.         else {
  2451.             DRichTextDoc* doc= new DRichTextDoc(0,  true, gTextFont);
  2452.             doc->Open(outputFile);
  2453.             aFile.Initialize(outputFile);
  2454.             aFile.Delete();
  2455.             }
  2456.          }
  2457.     aFile.Initialize(fastaInputFile);
  2458.     aFile.Delete();
  2459. }
  2460. #endif
  2461.  
  2462.  
  2463. Boolean DSeqDoc::DoMenuTask(long tasknum, DTask* theTask)
  2464. {
  2465.     DWindow* win = NULL;
  2466.     
  2467.     switch (tasknum) {
  2468.     
  2469.         case cNewSeq        : 
  2470.             AddNewSeqToList(); 
  2471.             //fall into EditSeqs
  2472.         case cEditSeq: 
  2473.             EditSeqs(); 
  2474.             return true;
  2475.  
  2476.         case cSaveSel:
  2477.             fSaveSelection= true;
  2478.             gApplication->DoMenuTask( DApplication::kSaveAs, NULL);
  2479.             fSaveSelection= false;
  2480.             return true;
  2481.  
  2482.         case cFindORF: 
  2483.             FindORF();
  2484.             return true;
  2485.             
  2486.         case cConsensus:
  2487.             MakeConsensus();
  2488.             return true;
  2489.                     
  2490.         case cRevSeq:
  2491.         case cCompSeq:
  2492.         case cRevCompSeq:
  2493.         case cDna2Rna: 
  2494.         case cRna2Dna:
  2495.         case cToUpper: 
  2496.         case cToLower:
  2497.         case cDegap:
  2498.         case cLockIndels:
  2499.         case cUnlockIndels:
  2500.         case cTranslate:
  2501.             {
  2502.             long    firstBase = 0, nBases = 0;
  2503.             DSeqList * aSeqList= NULL;
  2504.             DSeqChangeCmd *    cmd = NULL;  
  2505.             
  2506.             //fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  2507.             GetSelection( TRUE, TRUE, aSeqList, firstBase, nBases);
  2508.             switch (tasknum) {
  2509.               case cRevSeq        : cmd= new DSeqReverseCmd( this, fAlnView, aSeqList);  break;
  2510.                 case cCompSeq        : cmd= new DSeqComplementCmd( this, fAlnView,aSeqList); break;
  2511.                 case cRevCompSeq: cmd= new DSeqRevComplCmd( this, fAlnView,aSeqList); break;
  2512.                 case cDna2Rna        : cmd= new DSeqDna2RnaCmd( this, fAlnView,aSeqList); break;
  2513.                 case cRna2Dna        :    cmd= new DSeqRna2DnaCmd( this, fAlnView,aSeqList); break;
  2514.                 case cToUpper        :    cmd= new DSeqUppercaseCmd( this, fAlnView,aSeqList); break;
  2515.                 case cToLower        :    cmd= new DSeqLowercaseCmd( this,fAlnView, aSeqList); break;
  2516.                 case cDegap            :    cmd= new DSeqCompressCmd( this, fAlnView,aSeqList); break;
  2517.                 case cLockIndels:    cmd= new DSeqLockIndelsCmd( this, fAlnView,aSeqList); break;
  2518.                 case cUnlockIndels: cmd= new DSeqUnlockIndelsCmd( this, fAlnView,aSeqList); break;
  2519.                 case cTranslate    :    cmd= new DSeqTranslateCmd( this, fAlnView,aSeqList); break;
  2520.  
  2521.                 default:
  2522.                     delete aSeqList;
  2523.                     Message(MSG_OK,"DSeqDoc::method not ready.");
  2524.                     return true;
  2525.                 
  2526.               }
  2527.           if (cmd) {
  2528.               if (cmd->Initialize()) PostTask( cmd);
  2529.               else {
  2530.                   delete cmd;
  2531.                   Message(MSG_OK,"DSeqDoc::method failed.");
  2532.                     }
  2533.               }
  2534.             return true;    
  2535.             }
  2536.  
  2537.         case cPrettyPrint:  MakeSeqPrint( false); return true; //MakeAlnPrint();
  2538.         case cREMap:                MakeSeqPrint( true); return true;
  2539.         case cNAcodes:            NucCodesPicture(); return true;
  2540.         case cAAcodes:            AminoCodesPicture(); return true;
  2541.         case cDotPlot:            //MakeDottyPlot(); return true;
  2542.             Message(MSG_OK,"DSeqDoc::method not ready.");
  2543.             return true;
  2544.  
  2545. #if NETBLAST
  2546.         case cNCBIblast:
  2547.             {
  2548.             char* blastProgram = "blastn";
  2549.             char* blastDatabase = "nr";
  2550.             char* blastOptions = NULL;
  2551.             char* fastaInputFile = "spup-blast-input.fasta";
  2552.             char* outputFile = "spup-blast-result.text";
  2553.             char    instore[128];
  2554.             char  outstore[128];
  2555.             DFile aFile;
  2556.             
  2557.             fastaInputFile= DFileManager::TempFilename( instore);
  2558.             outputFile= DFileManager::TempFilename( outstore);
  2559.  
  2560.             short nseq= SelectionToFile( false, fastaInputFile, DSeqFile::kPearson);
  2561.             if (nseq) {
  2562.                 short err= CallNCBIBlastService(blastProgram, blastDatabase, blastOptions, 
  2563.                                                         fastaInputFile, outputFile);
  2564.  
  2565.                 if (err) {
  2566.                     Message(MSG_OK,"DSeqDoc::CallNCBIBlastService error %d", err);
  2567.                     }
  2568.                 else {
  2569.                     DRichTextDoc* doc= new DRichTextDoc(0,  true, gTextFont);
  2570.                     doc->Open(outputFile);
  2571.                     aFile.Initialize(outputFile);
  2572.                     aFile.Delete();
  2573.                     }
  2574.                  }
  2575.             aFile.Initialize(fastaInputFile);
  2576.             aFile.Delete();
  2577.             }
  2578.             return true;
  2579.             
  2580. #else        
  2581.         case cNCBIblast:
  2582. #endif
  2583.     
  2584.         case cGrailSearch:
  2585.         case cGeneidSearch:
  2586.       case cFHCRCblocks:
  2587.       case cEMBLfasta:
  2588.       case cEMBLblitz:
  2589.       case cEMBLquicks:
  2590.             {
  2591.             DSequence* aSeq= fAlnView->SelectedSequence();
  2592.             if (aSeq) {
  2593.                 switch (tasknum) {
  2594.                     case cNCBIblast: win= new DNCBIBlast(0, this, aSeq); break;
  2595.                      case cEMBLblitz: win= new DEMBLBlitz(0, this, aSeq); break;
  2596.                      case cEMBLfasta: win= new DEMBLFasta(0, this, aSeq); break;
  2597.                      case cEMBLquicks: win= new DEMBLQuicks(0, this, aSeq); break;
  2598.                      case cFHCRCblocks: win= new DFHCRCblocks(0, this, aSeq); break;
  2599.                      case cGeneidSearch: win= new DUWFGeneID(0, this, aSeq); break;
  2600.                      case cGrailSearch: win= new DORNLGrail(0, this, aSeq); break;
  2601.                     }
  2602.                 if (win) win->Open();
  2603.                 }
  2604.             return true;
  2605.             }
  2606.  
  2607.       case cFHCRCfetch:
  2608.         case cEMBLfetch:
  2609.         case cNCBIfetch:
  2610.             switch (tasknum) {
  2611.                 case cNCBIfetch: win= new DNCBIFetch(0, this); break;
  2612.                  case cEMBLfetch: win= new DEMBLFetch(0, this); break;
  2613.                  case cFHCRCfetch: win= new DFHCRCfetch(0, this); break;
  2614.                 }
  2615.             if (win) win->Open();
  2616.             return true;
  2617.                         
  2618.         case cGenmarkSearch:
  2619.         case cPythiaSearch:
  2620.             Message(MSG_OK,"DSeqDoc::method not ready.");
  2621.             return true;
  2622.  
  2623.  
  2624.         default: 
  2625.             return DTaskMaster::DoMenuTask(tasknum, theTask);
  2626.         }
  2627.  
  2628. }
  2629.  
  2630.  
  2631. Boolean DSeqDoc::IsMyAction(DTaskMaster* action) 
  2632. {
  2633.     long  menuid= 0, menuitem = action->Id();
  2634.     if (action->fSuperior) menuid = action->fSuperior->Id();
  2635.     
  2636.     //if (menuid == DApplication::cEditMenu)  
  2637.     switch (menuitem) {
  2638.     
  2639.         case DApplication::kCut:
  2640.         case DApplication::kCopy:
  2641.         case DApplication::kClear:
  2642.           {
  2643.             DAlnEditCommand* cmd= new DAlnEditCommand(this, menuitem);
  2644.             PostTask( cmd);
  2645.             return true;            
  2646.             }
  2647.         
  2648.         case DApplication::kPaste:
  2649.             {
  2650.           DView* clipview= (DView*) gClipboardMgr->fClipView; // GetClipView();
  2651.             if (clipview && clipview->fKind == DAlnView::kindAlnView) {
  2652.                 DAlnPasteCommand* cmd= new DAlnPasteCommand(this);
  2653.                 PostTask( cmd);
  2654.                 return true;            
  2655.                 }
  2656.             else if (HasEditText()) {  /* && clipview->fKind == OStype("TEXT") */
  2657.                 if (fEditText->IsMyAction(action)) return true;
  2658.                 }
  2659.             return true; //?? always handle this message?
  2660.             }
  2661.             
  2662.         case DApplication::kSelectAll:
  2663.             if (fAlnIndex) {
  2664.             Nlm_RecT r;
  2665.             Nlm_LoadRect(& r, 0, 0, 1/*fAlnIndex->GetMaxCols()*/, fAlnIndex->GetMaxRows());
  2666.             fAlnIndex->SelectCells( r);
  2667.             return true;            
  2668.             }
  2669.  
  2670.         }
  2671.  
  2672.         
  2673.     if (menuid == kViewKindMenu)  
  2674.      switch (menuitem) {
  2675.             case kViewByDefault:
  2676.                     this->SortView(DSeqList::kSortByItem); return true;
  2677.             case kViewByDate:
  2678.                     this->SortView(DSeqList::kSortByDate); return true;
  2679.             case kViewBySize:
  2680.                     this->SortView(DSeqList::kSortBySize); return true;
  2681.             case kViewByName:
  2682.                     this->SortView(DSeqList::kSortByName); return true;
  2683.             case kViewByKind:
  2684.                     this->SortView(DSeqList::kSortByKind); return true;
  2685.             case kViewAsText:
  2686.                     this->ToTextDoc(); return true;
  2687.             default: return true;
  2688.             }
  2689.  
  2690. #if MASKS         
  2691.     else if (menuid == kSeqMaskMenu && fAlnView->fMaskLevel>0)  {
  2692.         long iseq, nseq= fSeqList->GetSize();
  2693.          switch (menuitem) {
  2694.          
  2695.             case cMaskSelCommon:
  2696.                 fAlnView->HiliteCommonBases();
  2697.                 return true;
  2698.                 
  2699.             case cMaskSelORF:
  2700.                 fAlnView->HiliteORFs();
  2701.                 return true;
  2702.                 
  2703.             case cMaskSelAll:
  2704.                 for (iseq= 0; iseq<nseq; iseq++) 
  2705.                     fSeqList->SeqAt(iseq)->SetMask(fAlnView->fMaskLevel);
  2706.                 fAlnView->Invalidate();
  2707.                 return true;
  2708.                 
  2709.             case cMaskInvert:
  2710.                 for (iseq= 0; iseq<nseq; iseq++) 
  2711.                     fSeqList->SeqAt(iseq)->FlipMask(fAlnView->fMaskLevel);
  2712.                 fAlnView->Invalidate();
  2713.                 return true;
  2714.                 
  2715.             case cMaskClear:
  2716.                 for (iseq= 0; iseq<nseq; iseq++) 
  2717.                     fSeqList->SeqAt(iseq)->ClearMask(fAlnView->fMaskLevel);
  2718.                 fAlnView->Invalidate();
  2719.                 return true;
  2720.                 
  2721.             default: 
  2722.                 return true;
  2723.             }
  2724.         }
  2725. #endif
  2726.  
  2727.  
  2728.     else if (menuid == DSeqApps::kChildMenu) 
  2729.         switch (menuitem) {
  2730. #if 0
  2731.             case cAddChildCmd:
  2732.             case cRemoveChildCmd:  
  2733.                 DoMenuTask(menuitem);
  2734.                 break;
  2735. #endif    
  2736.             default:
  2737.                 {
  2738.                 long    firstBase = 0, nBases = 0;
  2739.                 DSeqList * aSeqList= NULL;
  2740.                 //fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  2741.                 GetSelection( TRUE, TRUE, aSeqList, firstBase, nBases);
  2742.                 DSeqApps::CallChildApp( menuitem, aSeqList);
  2743.                 if (aSeqList) delete aSeqList;
  2744.                 }
  2745.                 break;
  2746.             }
  2747.                 
  2748.     else if (menuitem == kColorButHit) {
  2749.         fUseColor= ((DView*)action)->GetStatus();
  2750.         fAlnView->Invalidate();
  2751.       return true;
  2752.       }
  2753.     else if (menuitem == kLockButHit) {
  2754.         fAlnView->SetTextLock( ((DView*)action)->GetStatus());
  2755.       return true;
  2756.       }
  2757.  
  2758. #if MASKS
  2759.     else if (menuitem == kModePopup) {
  2760.         short mode= fModePop->GetValue();  
  2761. #if 1
  2762.         fAlnView->SetViewMode( mode);
  2763. #else
  2764.         switch (mode) {
  2765.             case DAlnView::kModeSlide: 
  2766.                 fAlnView->SetTextLock( true);
  2767.                 if (fAlnView->fMaskLevel>0) fAlnView->Invalidate();
  2768.                 fAlnView->fMaskLevel= 0; 
  2769.                 break;
  2770.             case DAlnView::kModeEdit:  
  2771.                 fAlnView->SetTextLock( false);
  2772.                 if (fAlnView->fMaskLevel>0) fAlnView->Invalidate();
  2773.                 fAlnView->fMaskLevel= 0; 
  2774.                 break;
  2775.             case DAlnView::kModeMask1: 
  2776.             case DAlnView::kModeMask2: 
  2777.             case DAlnView::kModeMask3: 
  2778.             case DAlnView::kModeMask4: {
  2779.                 fAlnView->SetTextLock( true);
  2780.                 fAlnView->fMaskLevel= mode - DAlnView::kModeMask1 + 1; 
  2781.                 fAlnView->Invalidate();
  2782.                 short i, nseq= fSeqList->GetSize();
  2783.                 for (i=0; i<nseq; i++) fSeqList->SeqAt(i)->FixMasks();
  2784.                 }
  2785.                 break;
  2786.             }
  2787. #endif
  2788.       return true;
  2789.       }
  2790. #endif
  2791.     
  2792.     else 
  2793.         return DoMenuTask(menuitem, NULL);
  2794.         
  2795.     return false;
  2796. }
  2797.             
  2798.  
  2799.  
  2800.  
  2801.